activeMq

http://localhost:8161/admin/

admin/admin

 

1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>activedemo</groupId>
	<artifactId>activedemo</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>activedemo Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<distributionManagement>
		<downloadUrl>http://java.sun.com/products/jms/docs.html</downloadUrl>
	</distributionManagement>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- activemq 相关maven依赖 start -->
		<dependency>
			<groupId>javax.jms</groupId>
			<artifactId>jms</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-core</artifactId>
			<version>5.5.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.7.0</version>
		</dependency>
		<!-- activemq 相关maven依赖 end -->

		<!-- 日志相关依赖 start -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<!-- 日志相关依赖 end -->

	</dependencies>
	<build>
		<finalName>activedemo</finalName>
	</build>
</project>

2.消息发送者 


package com.active.mq2;

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

/**
 * 订阅消息的发送消息方
 * 
 * @author Administrator
 *
 */

public class MQProducer {
    public static void main(String[] args) throws InterruptedException {

        /**
         * 1.创建连接工厂 2.创建连接实例 3、启动连接
         * 4、创建session创建接收或发送的线程实例(创建session的时候定义是否要启用事务,
         * 且事务类型是Auto_ACKNOWLEDGE也就是消费者成功在Listern中获得消息返回时,会话自动确定用户收到消息)
         * 5、创建队列(消息发送的目的地) 6、创建消息发送者 7、创建消息 8、发送消息 9、session.commit();提交千万不要忘记了
         */

        ConnectionFactory connFactory = null;
        Connection conn = null;
        Session session = null;
        Destination destination = null;

        // 连接参数定义: 用户名 密码 url
        String name = "system";
        String password = "manager";
        String url = "failover://tcp://localhost:61616";

        System.out.println("消息发布者开始发布消息了……");
        try {

            // 创建连接工厂
            // 这里的连接参数可以使用常量:Constants.MQ_NAME, Constants.MQ_PASSWORD,
            // Constants.MQ_BROKETURL
            connFactory = new ActiveMQConnectionFactory(name, password, url);

            // 通过连接工厂创建连接实例
            conn = connFactory.createConnection();

            // 启动连接
            conn.start();

            // 4、创建session创建接收或发送的线程实例(创建session的时候定义是否要启用事务,且事务类型是Auto_ACKNOWLEDGE也就是消费者成功在Listern中获得消息返回时,会话自动确定用户收到消息)
            // 也可以使用:session = connection.createSession(Boolean.TRUE,
            // Session.AUTO_ACKNOWLEDGE);
            session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);

            // 创建队列,也就是消息发送的目的地
            destination = session.createTopic("FirstTopic");

            // 创建消息发布者
            MessageProducer messageProducer = session.createProducer(destination);

            // 创建需要发送的消息
            TextMessage textMessage = session.createTextMessage();
            
            for (int i = 0; i < 1000000; i++) {
                textMessage.setText("Hello,broadcast message NO.!" + i);
                // 发送消息
                messageProducer.send(textMessage);
                // 一定要记得这个,提交呀,
                session.commit();
            }
            System.out.println("消息发布者:" + textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {

            // 关闭连接
            if (conn != null) {
                try {
                    conn.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

 3.消息接收者1



package com.active.mq2;

import java.io.IOException;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 订阅消息的消息接收方01
 * 
 * @author Administrator
 *
 */
public class MQConsumer01 implements MessageListener {

    public static void main(String[] args) throws IOException {

        /**
         * 1.创建连接工厂 2.创建连接实例 3、启动连接 4、 创建接收或发送的线程实例(消费者就不需要开启事务了)
         * 5、创建队列(消息发送的目的地) 6、创建消息接收者 7、注册消息监听
         */

        ConnectionFactory connFactory = null;
        Connection conn = null;
        Session session = null;
        Destination destination = null;
        // 创建连接工厂需要的参数
        String name = "admin";
        String password = "admin";
        String url = "failover://tcp://localhost:61616";
        try {
            // 创建连接工厂
            connFactory = new ActiveMQConnectionFactory(name, password, url);
            // 创建连接实例
            conn = connFactory.createConnection();
            // 启动连接
            conn.start();
            // 创建session(创建接收或发送的线程实例(消费者就不需要开启事务了))
            session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
            // 创建消息目的地(消费者从这里读取消息)
            destination = session.createTopic("FirstTopic");
            // 创建消费者
            MessageConsumer messageConsumer = session.createConsumer(destination);
            // 消费者读取消息,监听消息
            messageConsumer.setMessageListener(new MQConsumer01());
            System.out.println("订阅者01已经准备好接收消息!");
            // //8、程序等待接收用户消息
            // System.in.read();
            // //9、关闭资源
            // messageConsumer.close();
            // session.close();
            // conn.close();

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    
    public void onMessage(Message message) {
        try{
            System.out.println("订阅者01接收到的消息为:"+((TextMessage) message).getText());
        }catch(JMSException e){
            e.printStackTrace();
        }
        
    }
}

4.消息接收者2

package com.active.mq2;

import java.io.IOException;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 订阅消息的消息接收方02
 * 
 * @author Administrator
 *
 */
public class MQConsumer02 implements MessageListener {

    public static void main(String[] args) throws IOException {

        /**
         * 1.创建连接工厂 2.创建连接实例 3、启动连接 4、 创建接收或发送的线程实例(消费者就不需要开启事务了)
         * 5、创建队列(消息发送的目的地) 6、创建消息接收者 7、注册消息监听
         */
        ConnectionFactory connFactory = null;
        Connection conn = null;
        Session session = null;
        Destination destination = null;

        // 创建连接工厂需要的参数
        String name = "admin";
        String password = "admin";
        String url = "failover://tcp://localhost:61616";
        try {
            // 创建连接工厂
            connFactory = new ActiveMQConnectionFactory(name, password, url);
            // 创建连接实例
            conn = connFactory.createConnection();
            // 启动连接
            conn.start();
            // 创建session(创建接收或发送的线程实例(消费者就不需要开启事务了))
            session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
            // 创建消息目的地(消费者从这里读取消息)
            destination = session.createTopic("FirstTopic");
            // 创建消费者
            MessageConsumer messageConsumer = session.createConsumer(destination);
            // 消费者读取消息,监听消息
            messageConsumer.setMessageListener(new MQConsumer02());
            System.out.println("订阅者02已经准备好接收消息!");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void onMessage(Message message) {
        try {
            System.out.println("订阅者02接收到的消息为:" + ((TextMessage) message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值