JMS TOPIC 代码备份
在使用JMS 发送消息到 目的地(TOPIC) 时,JMS 服务器使用weblogic 9.2 首先,做几步基础性的工作。
首先:要放一个weblogic.xml 文件到WEB-INF 下。然后修改web.xml 的dtd验正。weblogic 9.2 需要servlet 2.4的版本。
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
消息的发送机代码:
package com.jms.topic.test; import java.util.Properties; import javax.jms.DeliveryMode; import javax.jms.JMSException; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicPublisher; import javax.jms.TopicSession; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JmsSenderTopicTest { private static TopicConnection tConnection=null; public static void main(String[] args) { try { Properties p = new Properties(); p.put("Context.INITIAL_CONTEXT_FACTORY","weblogic.jndi.WLInitialContextFactory"); p.put(Context.PROVIDER_URL,"t3://localhost:7003");; p.put(Context.SECURITY_PRINCIPAL,"weblogic"); p.put(Context.SECURITY_CREDENTIALS,"weblogic"); Context ctx = new InitialContext(p); /**测试JNDI 是否联连成功 是否可以把属性挂在JNDI 树上 开始*/ ctx.bind("name","zs"); Object ojbObject = ctx.lookup("name"); System.out.println(ojbObject.toString()); /**测试JNDI 是否联连成功 是否可以把属性挂在JNDI 树上 结束*/ TopicConnectionFactory connectionFactory = (TopicConnectionFactory) ctx.lookup("testcf");//ConnectionFactory-0 tConnection = connectionFactory.createTopicConnection(); TopicSession tSession = tConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic) ctx.lookup("testTopic"); // 消息发送者 TopicPublisher tPublisher = null; // 会话 //根据队列建立消息发送者 tPublisher = tSession.createPublisher(topic); /***发送text 文本信息 开始**/ //建立消息 TextMessage tTextMessage= tSession.createTextMessage(); tSession.createTextMessage(); //开始连接 tConnection.start(); //开始发送消息 发送模式:DeliveryMode.NON_PERSISTENT tPublisher.setDeliveryMode(DeliveryMode.PERSISTENT); tTextMessage.setText(" 再发一次。go go go ..哈佛爱情故事"); tPublisher.send(tTextMessage); System.out.println("发送完毕"); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); }finally{ if(tConnection!=null){ try { tConnection.stop(); tConnection.close(); tConnection=null; } catch (JMSException e) { e.printStackTrace(); } } } } }
消息的接收机代码:
package com.jms.topic.test; import java.util.Properties; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicSession; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; public class JmsReceiveTopicTest2 extends HttpServlet{ private static final long serialVersionUID = -5395350304473151061L; @Override public void init() throws ServletException { Properties p = new Properties(); p.put("Context.INITIAL_CONTEXT_FACTORY","weblogic.jndi.WLInitialContextFactory"); p.put(Context.PROVIDER_URL,"t3://localhost:7003");; p.put(Context.SECURITY_PRINCIPAL,"weblogic"); p.put(Context.SECURITY_CREDENTIALS,"weblogic"); try { Context ctx= new InitialContext(p); /**测试JNDI 是否联连成功 是否可以把属性挂在JNDI 树上 开始*/ ctx.bind("name","zs"); Object ojbObject = ctx.lookup("name"); System.out.println(ojbObject.toString()); /**测试JNDI 是否联连成功 是否可以把属性挂在JNDI 树上 结束*/ TopicConnectionFactory connectionFactory = (TopicConnectionFactory) ctx.lookup("testcf"); TopicConnection tConnection = connectionFactory.createTopicConnection(); TopicSession tSession = tConnection.createTopicSession(true,Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic) ctx.lookup("testTopic"); MessageConsumer comsumer = tSession.createSubscriber(topic); comsumer.setMessageListener(new JMSListener()); tConnection.start(); System.out.println("接收开始2"); } catch (Exception e) { e.printStackTrace(); } super.init(); } }
package com.jms.topic.test; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.TextMessage; public class JMSListener implements MessageListener { String ident = null; public JMSListener(){} public JMSListener(String ident){ this.ident = ident; } public void onMessage(Message msg){ try {//--根据不同的消息类型接收消息 if (msg instanceof TextMessage) { if (ident == null) System.out.println(((TextMessage) msg).getText()); else System.out.println(ident + ": " + ((TextMessage) msg).getText()); } else if (msg instanceof ObjectMessage) { if (ident == null) System.out.println(((ObjectMessage) msg).getObject()); else System.out.println(ident + ": " + ((ObjectMessage) msg).getObject()); } }catch (JMSException jE) { System.err.println("Exception in listener: " + jE); } } }
把servlet 添加到web.xml 文件中。启动项目。当然,那些所需要的必备参数,要建在weblogic的 jms模块中。
<servlet> <servlet-name>jmsListener</servlet-name> <servlet-class>com.jms.topic.test.JmsReceiveTopicTest2</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
这样就可以发送消息的时候,消息接收机就会自动的接收消息。
注:消费接收机不能关闭connection 连接。需要一直保持与服务器的连接状态。