详细的请看:
深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例 http://blog.csdn.net/jiuqiyuliang/article/details/48608237
1.下载ActiveMQ
去官网下载apache-activemq-5.14.1-bin.zip,并解压。
链接:http://activemq.apache.org/activemq-5141-release.html
2.启动运行ActiveMQ
解压后进入bin目录,双击activemq.bat脚本文件。(或者可以根据自己电脑版本进入win32或win64目录运行activemq.bat)
进入:http://localhost:8161/admin 用户名密码都是admin
3.MyEclipse创建Java项目
需要导入ActiveMQ的jar包(解压后的根目录下的activemq-all-5.14.1.jar)
Producer.java:
- import javax.jms.Connection;
- import javax.jms.ConnectionFactory;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnection;
- import org.apache.activemq.ActiveMQConnectionFactory;
- /**
- * 消息生成者(发送者)
- * @author Administrator
- *
- */
- public class Producer {
- //默认连接用户名
- private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
- //默认连接密码
- private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
- //默认连接地址:tcp://localhost:61616
- private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;
- //发送的消息数量
- private static final int SENDNUM = 10;
- public static void main(String[] args) {
- //连接工厂,JMS 用它创建连接
- ConnectionFactory connectionFactory;
- //连接,JMS 客户端到JMS Provider 的连接
- Connection connection = null;
- //会话: 一个发送或接收消息的线程
- Session session;
- //消息的目的地;消息发送给谁
- Destination destination;
- //消息生产者
- MessageProducer messageProducer;
- //实例化连接工厂
- connectionFactory = new ActiveMQConnectionFactory(Producer.USERNAME, Producer.PASSWORD, Producer.BROKEURL);
- try {
- //通过连接工厂获取连接
- connection = connectionFactory.createConnection();
- //启动连接
- connection.start();
- //创建session
- session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
- //创建一个名称为HelloWorld的消息队列
- destination = session.createQueue("HelloWorld");
- //创建消息生产者
- messageProducer = session.createProducer(destination);
- //发送消息
- sendMessage(session, messageProducer);
- session.commit();
- }catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(connection != null){
- try {
- connection.close();
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 发送消息
- * @param session
- * @param messageProducer 消息生产者
- * @throws Exception
- */
- public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
- for (int i = 0; i < Producer.SENDNUM; i++) {
- //创建一条文本消息
- TextMessage message = session.createTextMessage("ActiveMQ 发送消息" +i);
- System.out.println("发送消息:Activemq 发送消息" + i);
- //通过消息生产者发出消息
- messageProducer.send(message);
- }
- }
- }
Consumer.java:
- import javax.jms.Connection;
- import javax.jms.ConnectionFactory;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MessageConsumer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnection;
- import org.apache.activemq.ActiveMQConnectionFactory;
- /**
- * 消息消费者(接收者)
- * @author Administrator
- *
- */
- public class Consumer {
- //默认连接用户名
- private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
- //默认连接密码
- private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
- //默认连接地址
- private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;
- public static void main(String[] args) {
- ConnectionFactory connectionFactory;//连接工厂
- Connection connection = null;//连接
- Session session;//会话 接受或者发送消息的线程
- Destination destination;//消息的目的地
- MessageConsumer messageConsumer;//消息的消费者
- //实例化连接工厂
- connectionFactory = new ActiveMQConnectionFactory(Consumer.USERNAME, Consumer.PASSWORD, Consumer.BROKEURL);
- try {
- //通过连接工厂获取连接
- connection = connectionFactory.createConnection();
- //启动连接
- connection.start();
- //创建session
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- //创建一个连接HelloWorld的消息队列
- destination = session.createQueue("HelloWorld");
- //创建消息消费者
- messageConsumer = session.createConsumer(destination);
- while (true) {
- //设置接收者接收消息的时间,为了便于测试,这里定为100s
- TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
- if(textMessage != null){
- System.out.println("收到的消息:" + textMessage.getText());
- }else {
- break;
- }
- }
- }catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(connection != null){
- try {
- connection.close();
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
4.运行
先运行Producer,再运行Consumer。
可以进入 http://localhost:8161/admin/queues.jsp 查看有关消息队列的信息。