- 浏览: 1594244 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (289)
- java 语法基础 (51)
- spring (8)
- mvc struct /Ant --build.xml (8)
- SOA (0)
- oracle 9i/10g (23)
- sql server 2000-2005 (3)
- 数据库基础知识 (6)
- 设计模式与软件架构 (10)
- Hibernate 持久化 (9)
- J2SE/J2EE/J2ME/AJAX 技术 (8)
- JSF 技术 (3)
- JAVA 图形化 (0)
- JMS (40)
- Eclipse 3.2 IDE 开发技巧 (13)
- 项目处理方法集合 (2)
- html/jsp/javascript (2)
- Unix/Linux (9)
- j2me/ARM/windriver/嵌入式 (4)
- 电信科学 (8)
- jsp (1)
- c/c++ (1)
- LZW压缩算法(java) (2)
- Android (77)
- 版本管理git/svn (2)
最新评论
-
huihai:
有demo吗?
NamingStrategy实现动态表名映射 -
cangbaotu:
推荐给大家一些有用的爬虫源码:https://github.c ...
网络爬虫(源代码参考) -
tuspark:
除了.classpath文件以外,.project文件也应该了 ...
Eclipse .classpath文件浅谈 -
tuspark:
造成eclipse自动关闭的原因有很多,这里有很多介绍:ecl ...
eclipse 自动关闭 解决方案 -
DEMONU:
网上都是这些,这种文章。。。
ActiveMQ中的消息持久性
import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQPrefetchPolicy; import org.apache.camel.component.jms.JmsMessage; import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; //发送TextMessage public class SendMessage { private static final String url = "tcp://localhost:61616";; private static final String QUEUE_NAME = "choice.queue"; protected String expectedBody = "<hello>world!</hello>"; public void sendMessage() throws JMSException{ Connection connection = null; try{ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(QUEUE_NAME); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(expectedBody); message.setStringProperty("headname", "remoteB"); producer.send(message); }catch(Exception e){ e.printStackTrace(); }finally{ connection.close(); } } *************************************************************************************** import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; //发送BytesMessage public class SendMessage { private String url = "tcp://localhost:61616"; public void sendMessage() throws JMSException{ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("test.queue"); MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); BytesMessage message = session.createBytesMessage(); byte[] content = getFileByte("d://test.jar"); message.writeBytes(content); try{ producer.send(message); System.out.println("successful send message"); }catch(Exception e){ e.printStackTrace(); e.getMessage(); }finally{ session.close(); connection.close(); } } private byte[] getFileByte(String filename){ byte[] buffer = null; FileInputStream fin = null; try { File file = new File(filename); fin = new FileInputStream(file); buffer = new byte[fin.available()]; fin.read(buffer); } catch (Exception e) { e.printStackTrace(); } finally { try { fin.close(); } catch (IOException e) { e.printStackTrace(); } } return buffer; }
发送完消息后可以访问
http://localhost:8161/admin/queues.jsp
看到相应的queue中是否有消息
适用收取TextMessage消息
import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class ReceiveMessage { private static final String url = "tcp://172.16.168.167:61616"; private static final String QUEUE_NAME = "szf.queue"; public void receiveMessage(){ Connection connection = null; try{ try{ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); connection = connectionFactory.createConnection(); }catch(Exception e){ // ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); // connection = connectionFactory.createConnection(); } connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(QUEUE_NAME); MessageConsumer consumer = session.createConsumer(destination); consumeMessagesAndClose(connection,session,consumer); }catch(Exception e){ } } protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer) throws JMSException { for (int i = 0; i < 1;) { Message message = consumer.receive(1000); if (message != null) { i++; onMessage(message); } } System.out.println("Closing connection"); consumer.close(); session.close(); connection.close(); } public void onMessage(Message message){ try{ if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage)message; String msg = txtMsg.getText(); System.out.println("Received: " + msg); } }catch(Exception e){ e.printStackTrace(); } } public static void main(String args[]){ ReceiveMessage rm = new ReceiveMessage(); rm.receiveMessage(); } }
评论
4 楼
GFJGH
2015-01-08
怎么收取的byte数组呢
3 楼
hundsun_2008
2012-10-25
怎么接受BytesMessage呢?BytesMessage肯定不能转换为textMessage 接收的
2 楼
jychenok
2011-08-24
楼上要设置respose.set编码
1 楼
herry
2009-06-10
不知道楼主用ActiveMQ发送过中文消息没?
我获取到得中文消息为乱码,不管是用TextMessage,还是BytesMessage,还是ObjectMessage
我获取到得中文消息为乱码,不管是用TextMessage,还是BytesMessage,还是ObjectMessage
发表评论
-
基于jms使用ActiveMQ实现异步日志功能.消息持久到oracle 10g 数据库
2008-09-18 15:05 9947package askyaya.entity;import j ... -
activemq 重新连接的机制
2008-09-18 14:39 23236最近一个项目要用到ActiveMq,并且需要最大程度的保证消息 ... -
ActiveMQ stop inactivity read check
2008-09-17 16:01 10716You can do the following to fix ... -
WSAD环境下JMS异步通信全攻略 (3)
2008-09-11 14:18 32473.5 消息驱动的Bean 在前文讨论JMS消息接收处理逻 ... -
WSAD环境下JMS异步通信全攻略 (2)
2008-09-11 13:58 3799三、JMS P2P编程 在JMS P2P通信方式中,发送程 ... -
WSAD环境下JMS异步通信全攻略 (1)
2008-09-11 13:57 4733一、JMS基本概念 1.1 P2P通信 1.2 Pub ... -
topicpublisher (jms)
2008-09-10 18:55 2936目的地类型JNDI名字连接工厂类型Topic/Queuejav ... -
ProducerTool /MessageBroker /getConnectionFactoryF
2008-09-10 18:12 2501lib: jms1.1.jar activemq-all-5. ... -
ActiveMQ in Action(7)
2008-09-10 13:44 3965ActiveMQ in Action(7) 关键字 ... -
ActiveMQ in Action(6)
2008-09-10 13:43 3748ActiveMQ in Action(6) 关键字: acti ... -
ActiveMQ in Action(5)
2008-09-10 13:42 3937ActiveMQ in Action(5) 关键字: acti ... -
ActiveMQ in Action(4)
2008-09-10 13:40 3491ActiveMQ in Action(4) 关键字: acti ... -
ActiveMQ in Action(3)
2008-09-10 13:39 3688ActiveMQ in Action(3) 关键字: acti ... -
ActiveMQ in Action(2)
2008-09-10 13:38 4223ActiveMQ in Action(2) 关键字: acti ... -
ActiveMQ in Action(1)
2008-09-10 13:37 5852ActiveMQ in Action(1) 关键字: acti ... -
为ActiveMQ服务器添加简单验证机制
2008-09-09 23:48 4208为ActiveMQ服务器添加简单验证机制 关键字: Java, ... -
Sender/receiver 消息
2008-09-09 20:28 2159Sender:import java.io.BufferedR ... -
activema.xml 配置
2008-09-09 17:44 3844/***作者:andyao,email:andyaoy@gma ... -
JMX 与系统管理
2008-09-08 10:52 1875Java SE 6 新特性: JMX 与系统管理 ... -
ActiveMQ中的消息持久性
2008-09-05 12:24 3408ActiveMQ中的消息持久性 ActiveMQ很 ...
相关推荐
这个简单的ActiveMQ例子可能是为了演示如何设置和使用基本的生产者和消费者,以及如何通过消息队列实现异步通信。在实际应用中,我们还可以利用ActiveMQ的高级特性,如持久化、优先级、消息筛选等,以满足更复杂的...
在"ActiveMQ学习"的完整例子中,你可以通过编写Java代码来创建生产者和消费者,实践发送和接收消息,了解消息的生命周期和状态。同时,通过配置不同的参数,体验ActiveMQ的灵活性和强大功能。例如,你可以创建一个...
6. **创建消息**: 在这个例子中,我们发送的是文件内容,所以消息可能是包含文件二进制数据的BytesMessage。 ```csharp var message = session.CreateBytesMessage(fileBytes); ``` 文件内容需要先读取到字节...
在实际开发过程中,可以通过编写Java代码来发送和接收消息,利用JMS API与ActiveMQ建立连接。以下是一个简单的JMS消息发送和接收的例子: ```java // 发送消息 ConnectionFactory connectionFactory = new ...
消息本身可以有多种类型,如TextMessage(文本消息)、ObjectMessage(对象消息)、BytesMessage(字节消息)、MapMessage(映射消息)和StreamMessage(流消息)。每种消息类型适用于不同的数据传输场景。 在JMS...
5. **示例代码**:在“jmstest”文件中,可能包含了一个简单的Java程序,它创建了生产者和消费者,并使用`ActiveMQ`这样的JMS提供商作为消息中间件。生产者会发送一个`TextMessage`到一个队列或主题,而消费者则监听...
常见的消息代理有ActiveMQ、RabbitMQ和Apache Kafka等。 4. **队列(Queue)**:提供一对一的消息传递模型,每个消息仅由一个消费者接收,确保消息顺序和至少一次交付。 5. **主题(Topic)**:提供一对多的消息...