1.在WEBLOGIC12中配置JMS服务器
参考链接:http://blog.csdn.net/gxlstone/article/details/41378949
2.创建java project编写测试代码
(1) 创建一个java project(不是web项目)
(2) 将weblogic安装目录中的wlclient.jar加入classpath中(注意:weblogic老版本的话使用weblogic.jar), 具体路径:$Weblogic_HOME\wlserver_10.3\server\lib。
(3) 需要将javaee.jar加入到classpath,因为我们建的是java project项目,不会自动添加javaee包。注意:从官网下载javaee sdk,安装后lib中有这个包。
package com.jms; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; //生产者 public class JMSSender { public static final String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"; private static QueueConnectionFactory qconFactory; private static QueueConnection qcon; private static QueueSession qsession; private static QueueSender qsender; private static Queue queue; private static TextMessage msg; public static void init() throws NamingException, JMSException { //init JNDI context String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";//define JNDI context factory String providerUrl = "t3://localhost:7001"; //define weblogic JMS url Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory); env.put(Context.PROVIDER_URL, providerUrl); Context ctx = new InitialContext(env); String queueName = "SevenQueue";//生成者队列 String connFactoryJNDI = "myJMSConnectionFactory";//工厂 //获取一个JMS连接工厂是为了获取与服务器的连接。前题是这个JMS工厂一定要事先创建好,注:查询的是连接工厂的JNDI的名字,不是连接工厂的名字。 qconFactory = ((QueueConnectionFactory) ctx.lookup(connFactoryJNDI)); // 创建一个列队或主题连接是为了获取与JMS会话的Session qcon = qconFactory.createQueueConnection(); // 创建会话Session 是为了创建一个消息的发送者和发送内容类型。 qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); //创建一个列队来存储消息,是消息的存方的物理地址,前题是这个队列需要在weblogic里创建后。 queue = ((Queue) ctx.lookup(queueName)); ///创建一个消息的发送者 qsender = qsession.createSender(queue); //创建一个发送内容类型(Stream,byte,map,text,object)。 msg = qsession.createTextMessage();//一个字符串对象 //打开连接,设置发送文本内容。 qcon.start(); ctx = null; } public static void send(String message) throws JMSException { msg.setText(message);//设置发送内容 qsender.send(msg); } public static void close() throws JMSException { if (qsender != null) { qsender.close(); } if (qsession != null) { qsession.close(); } if (qcon != null) qcon.close(); } /** * @param args * @throws NamingException * @throws JMSException */ public static void main(String[] args) throws NamingException, JMSException { init(); send("haha"); close(); } }
package com.jms; import java.util.Hashtable; import java.util.Map; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.dom4j.Element; import com.utils.BASE64; import com.utils.XmlHelper; //消费者 public class JMSReceiver { public static void main(String[] args) throws NamingException, JMSException { //init JNDI context String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";//define JNDI context factory String providerUrl = "t3://localhost:7001"; //define weblogic JMS url Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory); env.put(Context.PROVIDER_URL, providerUrl); Context ctx = new InitialContext(env); //find connection factory String connFactoryJNDI = "myJMSConnectionFactory"; //jms connectionFactory JNDI name QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(connFactoryJNDI); //create queue connection QueueConnection qConn = (QueueConnection) connFactory.createConnection(); //create session QueueSession qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); //find queue by JNDI lookup Queue queue = (Queue) ctx.lookup("SevenQueue"); //create receiver QueueReceiver qReceiver = qSession.createReceiver(queue); //create message listener qReceiver.setMessageListener(new MessageListener() { public void onMessage(Message msg) { String msgText = ""; double d = 0; try { if (msg instanceof TextMessage) { msgText = ((TextMessage) msg).getText(); } else if (msg instanceof StreamMessage) { msgText = ((StreamMessage) msg).readString(); d = ((StreamMessage) msg).readDouble(); } else if (msg instanceof BytesMessage) { byte[] block = new byte[1024]; ((BytesMessage) msg).readBytes(block); msgText = String.valueOf(block); } else if (msg instanceof MapMessage) { msgText = ((MapMessage) msg).getString("name"); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(msgText + " " + d); }}); qConn.start(); } }