ActiveMQ收发消息

1.下载ActiveMQ 

地址:http://activemq.apache.org/activemq-5153-release.html

2.运行ActiveMQ 
解压缩apache-activemq-5.15.3-bin.zip,然后双击apache-activemq-5.15.3\bin\win64\activemq.bat运行ActiveMQ程序。 

启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。 

3.创建项目,导入所需要的Jar包

结构如下:

ActiveMQ收发消息_第1张图片

Receiver.java

package com.deniel.test;

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

/**
 * 点对点模式--消息接收 单连接、消息不持久化、无事务、消息自动确认接收
 * 
 * @Author Deniel
 * @Date 2018.5.12
 */
public class Receiver {

	public static void main(String[] args) {
		ConnectionFactory connFactory;
		Connection conn = null;
		Session session = null;
		Destination destination;
		MessageConsumer consumer;

		try {
			connFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://127.0.0.1:61616");

			conn = connFactory.createConnection();
			conn.start();
			session = conn.createSession(Boolean.FALSE, // 不开启事务
					Session.AUTO_ACKNOWLEDGE); // 消息自动确认
			destination = session.createQueue("FirstQueue");
			consumer = session.createConsumer(destination);

			long startTime = System.currentTimeMillis();
			int i = 1;
			do {
				TextMessage message = (TextMessage) consumer.receive(100000);
				if (message != null) {
					System.out.println(i + " , 收到消息为: " + message.getText());
					i++;
				} else {
					System.out.println(i + " , 收到消息为null..... ");
				}
			} while (i <= 1000000);

			long endTime = System.currentTimeMillis();
			long diff = endTime - startTime;
			long diffSeconds = diff / 1000;

			System.out.print("receiver use time: " + diffSeconds + " seconds.");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

Sender.java

package com.deniel.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.ActiveMQConnectionFactory;

/**
 * 点对点模式--消息发送 单连接、消息不持久化、无事务、消息自动确认接收
 * 
 * @Author Deniel
 * @Date 2018.5.12
 */

public class Sender {
    private static final int SEND_NUMBER = 5;

    public static void main(String[] args) {
        // ConnectionFactory :连接工厂,JMS 用它创建连接
        ConnectionFactory connectionFactory;
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = null;
        // Session: 一个发送或接收消息的线程
        Session session;
        // Destination :消息的目的地;消息发送给谁.
        Destination destination;
        // MessageProducer:消息发送者
        MessageProducer producer;
        // TextMessage message;
        // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
        connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://127.0.0.1:61616");
        try {
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.TRUE,
                    Session.AUTO_ACKNOWLEDGE);
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
            destination = session.createQueue("FirstQueue");
            // 得到消息生成者【发送者】
            producer = session.createProducer(destination);
            // 设置不持久化,此处学习,实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 构造消息,此处写死,项目就是参数,或者方法获取
            sendMessage(session, producer);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }

    public static void sendMessage(Session session, MessageProducer producer)
            throws Exception {
        for (int i = 1; i <= SEND_NUMBER; i++) {
            TextMessage message = session
                    .createTextMessage("ActiveMq 发送的消息" + i);
            // 发送消息到目的地方
            System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
            producer.send(message);
        }
    }
}

你可能感兴趣的:(java)