jms 发送接收消息

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yare_522378972/article/details/84489355

jms 发送接收消息  

 
 
jms发送和接收消息的步骤
1.建立连接
ActiveMQConnectionFactory ac = new ActiveMQConnectionFactory();
// ac.setBrokerURL("tcp://localhost:61616?trace=false");
 
ConnectionFactory connectionFactory = ac;
 
2.获取session,
Connection connection = connectionFactory.createConnection();
connection.start();
 
Session session = connection.createSession(Boolean.TRUE,
Session.CLIENT_ACKNOWLEDGE);
 
3. 建立管道
Destination destination = session.createQueue("my-queue");
note:
建立了一个名字叫"my-queue"的queue的管道
 
4.创建消息和消息头
MessageProducer producer = session.createProducer(destination);
 
// 不要求JMS provider持久保存消息。
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
 
// 指示JMS provider持久保存消息,以保证消息不会因为JMS provider的失败而丢失
// producer.setDeliveryMode(DeliveryMode.PERSISTENT);
 
// 设置优先级
producer.setPriority(7);
 
// 设置是否记录消息被传递给send方法的时间,默认为记录
producer.setDisableMessageTimestamp(false);
 
System.out.println("connection");
// 发送消息
 
5.发送消息
// 创建一条文本消息
TextMessage textMessage = session.createTextMessage(message);
producer.send(textMessage);
 
// 创建一条map消息
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("name", "丽仪");
mapMessage.setString("sex", "女");
producer.send(mapMessage);
 
// 创建一条对象消息
ObjectMessage objMessage = session.createObjectMessage();
objMessage.setObjectProperty("school", "广大");
 
// 通过消息生产者发出消息
producer.send(objMessage);  
 
6.接收消息
while (true) {
Message message = consumer.receive(1000);
 
if (null != message) {
//区分不同的消息类型
if (message instanceof TextMessage)
System.out.println("收到TextMessage消息:"
+ ((TextMessage) message).getText());
 
if (message instanceof MapMessage)
System.out.println("收到MapMessage消息:"
+ ((MapMessage) message).getObject("name"));
 
if (message instanceof ObjectMessage)
System.out.println("收到ObjectMessage消息:"
+ ((ObjectMessage) message)
.getStringProperty("school"));
 
} else
break;
}
}
          
 
7.关闭连接
session.commit();
session.close();
connection.close();
展开阅读全文

如何保证JMS发送接收应用程序间的消息的正确顺序

03-21

下面是摘录的网上的一段关于JMS消息发送顺序的分组描述:rnJMS并不能始终保证发送和接收应用程序间的消息的正确顺序。如果三条消息按照顺序 A B C 发送,可能不会按照相同的顺序到达(例如,如果中间网络将消息分布到集群中,然后再重新组合时)。但如果应用程序希望按照发送顺序进行接收改如何呢?rn解决方式:通过消息分组来解决此问题。发送消息的应用程序可以指定其将消息 A、B 和 C 作为组的一部分发送。组中的每个消息都分配了一个序列号(从 1 开始)。然后,接收应用程序可以指定希望按照此逻辑顺序接收消息(与消息到达目的地的实际顺序相对)。现在,即使消息 B 或 C 首先到达,也不会将其立即传递给应用程序,因为它们的序列号不为 1。rnrn那么我的问题是:对于上面提到的分组在我的应用中并不能使用,对于可以分组的肯定是一次性操作发送的JMS消息,在我的应用中,消息A,B,C不是一次操作执行的,也就是说发送A后可能会隔很长时间才发送B,然后发送C,也有可能是发送A后立即发送B,一句话,是随机的按照A,B,C的顺序发送; 那么在异常情况,比如发送A,B后拔网线,然后发送C,这个时候JMS客户端接受的顺序变成的A,C,B,我抓了报文看了,确实是报文的顺序就已经错了(A,C,B),我希望的是A,B,C,rnrn请问在我上面这样的应用中如何才能保证JMS发送和接收应用程序间的消息的正确顺序呢? 比如像TCP那样保证发送的序列的可到性?rnrn补充:我使用的是JBOSS 4 自带的JMS服务器发送的; 论坛

jms 订阅方式接收不到消息!!!!!!

06-02

发送消息没问题,就是同步接收不到!!!! 跪求大神 在线等rnpackage com.fz.jms;rnrnimport java.util.Vector;rnrnimport javax.jms.*;rnrnimport org.apache.activemq.ActiveMQConnection;rnimport org.apache.activemq.ActiveMQConnectionFactory;rnrnpublic class TopicSubscribeSynchronous rnprivate String user = ActiveMQConnection.DEFAULT_USER; rn rn private String password = ActiveMQConnection.DEFAULT_PASSWORD; rn rn private String url = ActiveMQConnection.DEFAULT_BROKER_URL; rn rn private Topic topic = null; rn rn private TopicConnection topicConnection = null; rn rn private TopicSession topicSession = null; rn rn private TopicSubscriber topicSubscriber = null;rn rn //初始化rn private void initialize(String groupName) throws JMSException, Exception rn //连接工厂是用户创建连接的对象,这里使用的是ActiveMQ的ActiveMQConnectionFactory根据url,username和password创建连接工厂。 rn ActiveMQConnectionFactory TopicConnectionFactory = new ActiveMQConnectionFactory( rn user, password, url); rn //连接工厂创建一个jms connection rn topicConnection = TopicConnectionFactory.createTopicConnection(); rn //是生产和消费的一个单线程上下文。会话用于创建消息的生产者,消费者和消息。会话提供了一个事务性的上下文。 rn topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //不支持事务 rn //目的地是客户用来指定他生产消息的目标还有他消费消息的来源的对象,两种消息传递方式:点对点和发布/订阅 rn topic = topicSession.createTopic(groupName); rn //会话创建消息的生产者将消息发送到目的地 rn topicSubscriber = topicSession.createSubscriber(topic);rn rn rn public Vector subscriberMessage(String groupName) throws JMSException, Exception rn initialize(groupName); rn topicConnection.start(); rn rn try rn // 开始监听 rn //consumer.setMessageListener(this);rn Vector v=new Vector(10);rn// for (int i=0; i<10; i++) rn// rn //同步消息接收,使用receive方法,堵塞等待,直到接收消息rn TextMessage txtMsg = (TextMessage) topicSubscriber.receive();rn// if(txtMsg==null)rn// rn// break;rn// elsern// rn rn String msg = txtMsg.getText();rn System.out.println(msg);rn v.add(msg);rn// rn rn// rn return v;rn catch (Exception e) rn // TODO: handle exceptionrn e.printStackTrace();rn return null;rn finallyrn rn close();rn rn rn rn rn // 关闭连接 rn public void close() throws JMSException rn if (topicSubscriber != null) rn topicSubscriber.close(); rn if (topicSession != null) rn topicSession.close(); rn if (topicConnection != null) rn topicConnection.close(); rn rnrnrnrn 论坛

spring 配置jms 接收weblogic消息

07-03

我使用spring jmsTemplate进行配置接收远程weblogic的jms消息出错rn有没有人可以指点一二rn严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerrnorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsConnectionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 203 completed: Norn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)rn at java.security.AccessController.doPrivileged(Native Method)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)rn at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)rn at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)rn at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)rn at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)rn at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)rn at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)rn at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)rn at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)rn at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:926)rn at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:889)rn at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)rn at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)rn at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)rn at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)rn at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)rn at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)rn at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)rn at org.apache.catalina.core.StandardService.start(StandardService.java:516)rn at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)rn at org.apache.catalina.startup.Catalina.start(Catalina.java:578)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)rn at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)rnCaused by: org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 203 completed: Norn at com.sun.corba.se.impl.logging.ORBUtilSystemException.writeErrorSend(ORBUtilSystemException.java:2231)rn at com.sun.corba.se.impl.logging.ORBUtilSystemException.writeErrorSend(ORBUtilSystemException.java:2253)rn at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.writeLock(SocketOrChannelConnectionImpl.java:933)rn at com.sun.corba.se.impl.encoding.BufferManagerWriteStream.sendFragment(BufferManagerWriteStream.java:78)rn at com.sun.corba.se.impl.encoding.BufferManagerWriteStream.sendMessage(BufferManagerWriteStream.java:96)rn at com.sun.corba.se.impl.encoding.CDROutputObject.finishSendingMessage(CDROutputObject.java:144)rn at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.finishSendingRequest(CorbaMessageMediatorImpl.java:247)rn at com.sun.corba.se.impl.protocol.CorbaClientRequestDispatcherImpl.marshalingComplete1(CorbaClientRequestDispatcherImpl.java:342)rn at com.sun.corba.se.impl.protocol.CorbaClientRequestDispatcherImpl.marshalingComplete(CorbaClientRequestDispatcherImpl.java:323)rn at com.sun.corba.se.impl.protocol.CorbaClientDelegateImpl.invoke(CorbaClientDelegateImpl.java:129)rn at com.sun.corba.se.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:213)rn at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)rn at weblogic.corba.j2ee.naming.Utils.narrowContext(Utils.java:126)rn at weblogic.corba.j2ee.naming.InitialContextFactoryImpl.getInitialContext(InitialContextFactoryImpl.java:94)rn at weblogic.corba.j2ee.naming.InitialContextFactoryImpl.getInitialContext(InitialContextFactoryImpl.java:31)rn at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:41)rn at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)rn at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)rn at javax.naming.InitialContext.init(InitialContext.java:223)rn at javax.naming.InitialContext.(InitialContext.java:197)rn at org.springframework.jndi.JndiTemplate.createInitialContext(JndiTemplate.java:114)rn at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:86)rn at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:130)rn at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:155)rn at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)rn at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)rn at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:193)rn at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:179)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)rn ... 39 more 论坛

没有更多推荐了,返回首页