1、启动ActiveMQ服务器
2、消息的发送
import java.util.Date; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * 消息的发送者 */ public class Producer { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; String url = ActiveMQConnection.DEFAULT_BROKER_URL; String subject = "TOOL.DEFAULT"; /**创建连接工厂*/ ActiveMQConnectionFactory contectionFactory = new ActiveMQConnectionFactory( user, password, url); try { /**创建连接*/ Connection connection = contectionFactory.createConnection(); connection.start();//开启连接 /**创建session*/ Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); /**指定消息的目的地*/ Destination destination = session.createQueue(subject); /**创建发送消息对象*/ MessageProducer producer = session.createProducer(destination); for (int i = 0; i <= 2; i++) { MapMessage message = session.createMapMessage(); Date date = new Date(); message.setLong("date", date.getTime()); message.setString("seqnum", String.valueOf(i)); Thread.sleep(1000); producer.send(message); System.out.println("发送端发送消息:" + date); } session.commit(); session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
3、消息的消费
/** * queue:消息消费者 * * @author 文超 * @version [版本号, Apr 27, 2013 5:28:49 PM ] * @see [相关类/方法] * @since [产品/模块版本] */ public class Consumer { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; String url = ActiveMQConnection.DEFAULT_BROKER_URL; String subject = "TOOL.DEFAULT"; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); Connection connection; try { connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(subject); MessageConsumer message = session.createConsumer(destination); message.setMessageListener(new MessageListener() { public void onMessage(Message msg) { MapMessage message = (MapMessage) msg; try { System.out.println("接收端收到消息," +message.getString("seqnum")+":"+ new Date(message.getLong("data"))); session.commit(); } catch (JMSException e) { e.printStackTrace(); } } }); Thread.sleep(3000); session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
相关推荐
client.publish('test/topic', 'Hello, MQTT World!'); }); // 接收消息事件 client.on('message', function (topic, message) { console.log(`Received message from topic ${topic}: ${message.toString()}`); ...
的基础知识,并且讲解了一个HelloWorld级别的例子帮助理解。这是关于消息中间件ActiveMQ的一个系列专题文章,将涵盖JMS、ActiveMQ的初步入门及API详细使用、两种经典的消息模式(PTP andPub/Sub)、与Spring整合、...
sender.sendMessage("Hello, World!"); // 保持应用运行,以便接收消息 context.registerShutdownHook(); } } ``` 这个例子展示了如何在Spring应用中使用Spring JMS和ActiveMQ创建一个简单的消息发送和接收...
ActiveMQ 使用例子,包含静态库和头文件,VS2015 编译。 // testActiveMQ.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" /* * Licensed to the Apache Software Foundation (ASF) under one or ...
package cn.itcast.mq.helloworld; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import org.junit.Test; import java.io....
stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); }); function showGreeting(message) { var g = document.getElementById('greetings'); var el = document.createElement('div');...
- **简单应用示例**:提供了一些简单的应用实例,帮助用户更好地理解和使用 Mule,如 HelloWorld 示例、贷款代理(Loan Broker)示例等。 - **邮件、Servlet 和 ActiveMQ 示例**:通过具体的示例,展示了如何利用 ...
以下是一个简单的例子: 1. 在Spring Boot服务端,你需要配置WebSocket和STOMP: ```java @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements ...
stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); ``` 在这个例子中,`showGreeting` 是一个自定义函数,用于显示接收到的服务器消息。 总的来说,`sockjs.min.js` 和 `stomp.min.js` 的...