ActiveMQ学习(二)ActiveMQ的消息形式

对于消息的传递有两种类型:

1.一种是点对点的(Queue),即一个生产者和一个消费者一一对应;

2.另一种是发布/订阅模式(Topic),即一个生产者产生消息并进行发送后,可以由多个消费者进行接收。

一, 点对点式消息队列(Queue ):如果没有人处理,就会持久化储存知道有人来处理

消息生产者

package com.easytop.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * name:zengjun
 * Date: 2018/11/5
 * Time: 10:40
 * Version:1.0
 * impact:
 */
public class QueueConsumer {

    public static void main(String[] args) {
        //连接信息设置
        String username = "system";
        String password = "manager";
        String brokerURL = "failover://tcp://localhost:61616";
        //连接工厂
        ConnectionFactory connectionFactory = null;
        //连接
        Connection connection = null;
        //会话 接受或者发送消息的线程
        Session session = null;
        //消息的目的地
        Destination destination = null;
        //消息消费者
        MessageConsumer messageConsumer = null;
        //实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);

        try {
            //通过连接工厂获取连接
            connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //创建一个连接QueueTest的消息队列
            destination = session.createQueue("QueueTest");
            //创建消息消费者
            messageConsumer = session.createConsumer(destination);
			//为了能够多次发送
            while (true) {
                TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
                if (textMessage != null) {
                    System.out.println("成功接收消息:" + textMessage.getText());
                } else {
                    break;
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

消息消费者

package com.easytop.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Scanner;

/**
 * name:zengjun
 * Date: 2018/11/5
 * Time: 9:47
 * Version:1.0
 * impact:
 */
public class QueueDemo {

    public static void main(String[] args) {

    //连接信息设置
     String username = "system";

     String password ="manager";

     String borkerURL="failover://tcp://192.168.4.187:61616";

    //工厂类
    ConnectionFactory connectionFactory= null;

    //连接
    Connection connection= null;

    //会话
    Session session= null;

    //消息的目的地
    Destination destination= null;

    //消息生产者
    MessageProducer messageProducer=null;

    //实例化连接工厂

    connectionFactory=  new ActiveMQConnectionFactory(username,password,borkerURL);


        try {
            //创建连接
            Connection factoryConnection = connectionFactory.createConnection();

            //开始链接
            factoryConnection.start();

            //创建session
            session  = factoryConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);

            destination = session.createQueue("QueueTest");

            messageProducer = session.createProducer(destination);

            TextMessage message = null;

            boolean a= true;
            System.out.println("请输入你想要发送的消息(按0退出)");
            while (a){
                Scanner scanner= new Scanner(System.in);

                String tmpMessage = scanner.nextLine();
                //创建发送的的文本信息

                if ("0".equals(tmpMessage)){
                    a=false;
                }else{
                    message = session.createTextMessage("Queue消息测试:"+tmpMessage);

                    messageProducer.send(message);

                    System.out.println("发送成功!"+message.getText());
                }
                //需要session提交了之后另一端才能接收到消息
                session.commit();
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if (null!=connection){
                try {
                    connection.close();
                }catch (JMSException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果为:
在这里插入图片描述
在这里插入图片描述

二, 主题发布订阅式(Topic): 过时不候

主题发布者

package com.easytop.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Scanner;

/**
 * name:zengjun
 * Date: 2018/11/5
 * Time: 15:27
 * Version:1.0
 * impact:
 */
public class TopicDemo {
    public static void main(String[] args) {

        //连接信息设置
        String username = "system";

        String password ="manager";

        String borkerURL="failover://tcp://localhost:61616";

        //工厂类
        ConnectionFactory connectionFactory= null;

        //连接
        Connection connection= null;

        //会话
        Session session= null;

        //消息主题
        Topic topic = null;

        //消息生产者
        MessageProducer messageProducer=null;

        //实例化连接工厂

        connectionFactory=  new ActiveMQConnectionFactory(username,password,borkerURL);


        try {
            //创建连接
//            Connection factoryConnection = connectionFactory.createConnection();

            //通过工厂获得连接
            connection=connectionFactory.createConnection();

            //开始链接
            connection.start();

            //创建session
            session  = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

            Topic topicTest = session.createTopic("TopicTest");

            messageProducer = session.createProducer(topicTest);

            //不将数据持久化保存
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            TextMessage message = null;

            boolean a= true;
            System.out.println("请输入你想要发送的消息(按0退出)");
            while (a){
                Scanner scanner= new Scanner(System.in);

                String tmpMessage = scanner.nextLine();
                //创建发送的的文本信息

                if ("0".equals(tmpMessage)){
                    a=false;
                }else{
                    message = session.createTextMessage("Topic消息测试:"+tmpMessage);

                    messageProducer.send(message);

                    System.out.println("发送成功!"+message.getText());
                }
                session.commit();
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if (null!=connection){
                try {
                    connection.close();
                }catch (JMSException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

消息订阅者

package com.easytop.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * name:zengjun
 * Date: 2018/11/5
 * Time: 15:28
 * Version:1.0
 * impact:
 */
public class TopicConsumer {
    public static void main(String[] args) {
        //连接信息设置
        String username = "system";
        String password = "manager";
        String brokerURL = "failover://tcp://localhost:61616";
        //连接工厂
        ConnectionFactory connectionFactory = null;
        //连接
        Connection connection = null;
        //会话 接受或者发送消息的线程
        Session session = null;

        //消息主题
        Topic topic =null;

        //消息消费者
        MessageConsumer messageConsumer = null;
        //实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);

        try {
            //通过连接工厂获取连接
            connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //创建一个连接Topic的消息队列
            topic = session.createTopic("TopicTest");
            //创建消息消费者
            messageConsumer = session.createConsumer(topic);

            messageConsumer.setMessageListener(new MyMessageListener());

        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

class MyMessageListener implements MessageListener {

    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("接收订阅主题:" + textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

运行结果:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值