J2EE----JMS
来源:互联网 发布:mac键盘锁住了怎么解锁 编辑:程序博客网 时间:2024/05/24 06:35
1、JMS是一个由AS提供的Message服务。它能接受消息产生者(Message Provider)所发出的消息,并把消息转发给消息消费者(Message Consumer)。
2、JMS提供2种类型的消息服务:(1)Queue,即点对点,每个消息只转发给一个消息消费者使用。(2)Topic,即发布和订阅,每个消息可以转发给所有的订阅者(消费者)。
3、jboss下的JMS配置:
(1)配置JMS Connection Factory(2)配置JMS File Store
(3)配置JMS Server
(4)在JMS Server的destinations中配置JMS Queue或者JMS Topic
其中提供给消息产生者和消息消费者使用的是JMS Connection Factory的JNDI和JMS Queue或者JMS Topic的JNDI。
4、消息产生者向JMS发送消息的步骤:
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic)(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息生产者MessageSender
(5)使用消息生产者MessageSender发送消息
一个消息发送者的例子:
package myjms;import java.util.*;import javax.naming.*;import javax.jms.*;public class MessageProducter { public static void main(String[] args) { String queueConnectionFactoryName = "myjmsconnectionfactory"; //JMS Connection Factory的JNDI String queueName = "myjmsqueue"; //JMS Queue或者JMS Topic的JNDI boolean transacted = false;//transaction模式 int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式 String message="Message need to send";//模拟需要发送的消息 Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, "t3://localhost:7001"); try { Context context = new InitialContext(properties); Object obj = context.lookup(queueConnectionFactoryName); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的获得 obj = context.lookup(queueName); Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的获得 QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//产生连接 queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode); TextMessage textMessage = queueSession.createTextMessage(); textMessage.clearBody(); textMessage.setText(message); QueueSender queueSender = queueSession.createSender(queue); queueSender.send(textMessage); if (transacted) { queueSession.commit(); } if (queueSender != null) { queueSender.close(); } if (queueSession != null) { queueSession.close(); } if (queueConnection != null) { queueConnection.close(); } } catch(Exception ex){ ex.printStackTrace(); } }}
5、消息消费者从JMS接受消息的步骤:
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic)(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver
(5)使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver
消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。
一个消息消费者的例子:
package myjms;import java.util.*;import javax.naming.*;import javax.jms.*;public class MessageReciever implements MessageListener { public void onMessage(Message message) { if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; try { System.out.println("Message content is:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } public static void main(String[] args) { MessageReciever msgRcvr=new MessageReciever(); String queueConnectionFactoryName = "myjmsconnectionfactory"; String queueName = "myjmsqueue"; boolean transacted = false; int acknowledgementMode = Session.AUTO_ACKNOWLEDGE; Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, "t3://localhost:7001"); try { Context context = new InitialContext(properties); Object obj = context.lookup(queueConnectionFactoryName); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj; obj = context.lookup(queueName); Queue queue = (Queue) obj; QueueConnection queueConnection = queueConnectionFactory. createQueueConnection(); queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode); QueueReceiver queueReceiver = queueSession.createReceiver(queue); queueReceiver.setMessageListener(msgRcvr); synchronized(msgRcvr){ msgRcvr.wait(100000); } if (queueReceiver != null) { queueReceiver.close(); } if (queueSession != null) { queueSession.close(); } if (queueConnection != null) { queueConnection.close(); } } catch (Exception ex) { ex.printStackTrace(); } }}
0 0
- J2EE----JMS
- j2ee之JMS(一)
- J2EE——JMS
- J2EE核心技术-JMS和JDBC
- J2EE总结------JMS消息服务
- J2EE技术规范(九)——JMS (JMS客户端)
- 运用JMS构建J2EE异构系统
- J2EE技术(三)——JMS
- J2EE探索者:用JMS进行企业消息传递[Z]
- J2EE探索者: 用JMS进行企业消息传递
- J2EE技术规范(八)——JMS(消息,域)
- 【J2EE】企业级项目开发总结--JMS基础篇
- 【J2EE】企业级项目开发总结--JMS点对点模型篇
- 【J2EE】企业级项目开发总结--JMS发布-订阅模型篇
- JMS
- JMS
- JMS
- JMS
- 快速线性筛法求素数
- Eclipse中10个最有用的快捷键组合
- 10、head()函数
- JavaScript的计时器的工作原理
- OpenGL Shading Language 入门之 Hello World
- J2EE----JMS
- VCF虚拟融合架构--企业IT基础架构建设新趋势-李飞 秦贵松 (VCF 虚拟融合架构 )
- 关于七牛上传701错误码解释
- 排序算法总结
- CAS 与 Spring Security 3.1整合配置详解
- (5.1.4)计算机网络基础之网络层
- webapp前端框架汇总
- angularjs学习系(2)
- 如何切割 合并文件