`
yufenfei
  • 浏览: 801020 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ActiveMQ之四:HelloWorld例子

阅读更多

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();
		}
	}
}

 

分享到:
评论
1 楼 tonytony3 2014-10-22  
it works ,thank you

相关推荐

    mqttjs(activemq测试工具)

    client.publish('test/topic', 'Hello, MQTT World!'); }); // 接收消息事件 client.on('message', function (topic, message) { console.log(`Received message from topic ${topic}: ${message.toString()}`); ...

    ActiveMQ从入门到精通(一)

    的基础知识,并且讲解了一个HelloWorld级别的例子帮助理解。这是关于消息中间件ActiveMQ的一个系列专题文章,将涵盖JMS、ActiveMQ的初步入门及API详细使用、两种经典的消息模式(PTP andPub/Sub)、与Spring整合、...

    spring-jms使用queue发送消息简单例子

    sender.sendMessage("Hello, World!"); // 保持应用运行,以便接收消息 context.registerShutdownHook(); } } ``` 这个例子展示了如何在Spring应用中使用Spring JMS和ActiveMQ创建一个简单的消息发送和接收...

    testActiveMQ.rar

    ActiveMQ 使用例子,包含静态库和头文件,VS2015 编译。 // testActiveMQ.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" /* * Licensed to the Apache Software Foundation (ASF) under one or ...

    MQ学习文档 方便回顾!

    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....

    springWebsocket的简单例子

    stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); }); function showGreeting(message) { var g = document.getElementById('greetings'); var el = document.createElement('div');...

    mule-1.4.4-getting-started.pdf

    - **简单应用示例**:提供了一些简单的应用实例,帮助用户更好地理解和使用 Mule,如 HelloWorld 示例、贷款代理(Loan Broker)示例等。 - **邮件、Servlet 和 ActiveMQ 示例**:通过具体的示例,展示了如何利用 ...

    WebSocket的两种简单实现

    以下是一个简单的例子: 1. 在Spring Boot服务端,你需要配置WebSocket和STOMP: ```java @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements ...

    sockjs.min.js+stomp.min.js

    stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); ``` 在这个例子中,`showGreeting` 是一个自定义函数,用于显示接收到的服务器消息。 总的来说,`sockjs.min.js` 和 `stomp.min.js` 的...

    JAVA上百实例源码以及开源项目源代码

    Java访问权限控制源代码 1个目标文件 摘要:Java源码,文件操作,权限控制 Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流...

Global site tag (gtag.js) - Google Analytics