Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ActiveMQ message passing in Apache

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you Apache ActiveMQ messaging sample analysis, I believe most people do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!

1. Download ActiveMQ

Download activeMQ: activemq.apache.org/

I'm using version 5.9.0 here.

2. Running ActiveMQ

Unzip apache-activemq-5.5.1-bin.zip and double-click apache-activemq-5.5.1\bin\activemq.bat to run ActiveMQ.

After you start ActiveMQ, log in to http://localhost:8161/admin/and create a Queue named FirstQueue.

3. Create Eclipse project and run

Create project: ActiveMQ, and import the jar files required under apache-activemq-5.9.0\lib directory. The project structure is shown in the following figure:

3.1.Sender.javapackage per.activemq.test;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender { private static final int SEND_NUMBER = 5; public static void main(String[] args) { // ConnectionFactory: Connection factory, JMS uses it to create connections ConnectionFactory connectionFactory; // Connection: JMS client to JMS provider connection Connection connection = null; // Session: A thread that sends or receives messages Session session; // Destination: Destination of the message; to whom the message is sent. Destination destination; // MessageProducer: Message sender MessageProducer producer; //Construct ConnectionFactory instance object, here using ActiveMq implementation jar connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://127.0.0.1:61616"); try { //construct connection object from factory connection = connectionFactory.createConnection(); //Start connection.start(); //Get operation connection session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //Get session Note that the parameter value xingbo.xu-queue is a queue of servers, which must be configured in the console of ActiveMq destination = session.createQueue("FirstQueue"); //Get message generator [sender] producer = session.createProducer(destination); //Settings are not persistent, learn here, actually decide according to project producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //construct message, write dead here, item is parameter, or method get sendMessage(session,producer); session.commit(); } catch (Exception e) { // TODO: handle exception } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } } private static void sendMessage(Session session, MessageProducer producer) throws Exception { for (int i = 0; i

< SEND_NUMBER; i++) { TextMessage message = session.createTextMessage("ActiveMQ发送的消息:"+i); // 发送消息到目的地方 System.out.println("发送消息,ActiveMQ发送的消息:"+i); producer.send(message); } }}3.2.Receiver.javapackage per.activemq.test;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver { public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // 消费者,消息接收者 MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://127.0.0.1:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); while (true) { //设置接收者接收消息的时间,为了便于测试,这里谁定为100s TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { System.out.println("收到消息" + message.getText()); } else { break; } } } catch (Exception e) { // TODO: handle exception } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }}4.注意事项 最后接收者跟发送者在不同的机器上测试 项目所引用的jar最后在ActiveMQ下的lib中找,这样不会出现版本冲突。 5.测试过程刚开始运行Receiver以后console介面没有任何信息,运行Sender以后,Receiver中的console显示如下信息: 发送消息:ActiveMq 发送的消息1 发送消息:ActiveMq 发送的消息2 发送消息:ActiveMq 发送的消息3 发送消息:ActiveMq 发送的消息4 发送消息:ActiveMq 发送的消息5 而回到Sender中发现console界面出现如下信息: 收到消息ActiveMq 发送的消息1 收到消息ActiveMq 发送的消息2 收到消息ActiveMq 发送的消息3 收到消息ActiveMq 发送的消息4 收到消息ActiveMq 发送的消息5 5.查看队列

Apparently, there's news.

That's all for "Sample Analysis of ActiveMQ Messaging in Apache." Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report