qmproducer.java

来自「网络购物系统开发详细步骤」· Java 代码 · 共 52 行

JAVA
52
字号
package jms.client;

import javax.jms.QueueConnectionFactory;
import javax.jms.*;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Queue;
import javax.jms.QueueSender;
import javax.jms.TextMessage;
import javax.jms.JMSException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.util.Properties;

public class QMProducer {
  public static void main(String[] args) {
    Properties jndiEnv = new Properties();
    jndiEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                        "weblogic.jndi.WLInitialContextFactory");
    jndiEnv.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
    QueueConnection con = null;
    QueueSession session = null;
    QueueSender sender = null;
    try {
      Context ctx = new InitialContext(jndiEnv);
      QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup(
          "jms/QueueConnectionFactory");
      con = factory.createQueueConnection();
      session = con.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
      Queue q = (Queue) ctx.lookup("jms/fileQueue");
      sender = session.createSender(q);
      TextMessage msg = session.createTextMessage();
      msg.setText("Hello World");
      sender.send(msg);
    }
    catch (NamingException ne) {
      ne.printStackTrace();
    }
    catch (JMSException je) {
      je.printStackTrace();
    }
    finally{
      if(sender !=null)try{sender.close();}catch(JMSException je){}
      if(session !=null)try{session.close();}catch(JMSException je){}
      if(con !=null)try{con.close();}catch(JMSException je){}

    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?