ActiveMq基础知识部分

1.下载地址 https://activemq.apache.org/components/classic/download/
2.创建生产者

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
 * @program: activemq
 * @author: dolphin
 * @create: 2019-07-31 10:04
 * @description: 消息的生产者
 **/
public class Producer {
    public static void main(String[] args) {
        ActiveMQConnectionFactory activeMQConnectionFactory=null;
        Connection connection=null;
        Session session=null;
        try{
            // 1、创建连接工厂
            activeMQConnectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");
            // 2、创建连接对象
            connection = activeMQConnectionFactory.createConnection();
            connection.start();

            // 3、创建会话
            // 第一个参数:是否支持事务,如果为true,则会忽略第二个参数,被jms服务器设置为SESSION_TRANSACTED
            // 第一个参数为false时,第二个参数的值可为Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE其中一个。
            // Session.AUTO_ACKNOWLEDGE为自动确认,客户端发送和接收消息不需要做额外的工作。哪怕是接收端发生异常,也会被当作正常发送成功。
            // Session.CLIENT_ACKNOWLEDGE为客户端确认。客户端接收到消息后,必须调用javax.jms.Message的acknowledge方法。jms服务器才会当作发送成功,并删除消息。
            // DUPS_OK_ACKNOWLEDGE允许副本的确认模式。一旦接收方应用程序的方法调用从处理消息处返回,会话对象就会确认消息的接收;而且允许重复确认。
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // 4、创建点对点发送的目标
            Destination destination = session.createQueue("queue1");
            // 创建发布的目标
           Destination destination1 = session.createTopic("topic1");

            // 5、创建生产者消息
            MessageProducer producer = session.createProducer(destination);
            // 设置生产者的模式,有两种可选
            // DeliveryMode.PERSISTENT 当activemq关闭的时候,队列数据将会被保存
            // DeliveryMode.NON_PERSISTENT 当activemq关闭的时候,队列里面的数据将会被清空
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            // 6、创建一条消息
            // 有6中消息类型:
            // BytesMessage  用来传递字节
            // MapMessage    用来传递字节
            // ObjectMessage 用来传递序列化对象
            // StreamMessage 用来传递文件等
            // TextMessage   用来传递字符串
            String text = "Hello world!";
            TextMessage message = session.createTextMessage(text);

            // 7、发送消息
            producer.send(message);

        }catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e1) {
                    e1.printStackTrace();
                }
            }

            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e1) {
                    e1.printStackTrace();
                }
            }
        }

    }
}

3.创建消费者

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

/**
 * @program: activemq
 * @author: dolphin
 * @create: 2019-07-31 09:45
 * @description: 消费者
 **/
public class Consumer {
    public static void main(String[] args) {
        ActiveMQConnectionFactory activeMQConnectionFactory=null;
        Connection connection=null;
        Session session=null;
        MessageConsumer consumer=null;
        try{
            //1.创建连接工厂
            activeMQConnectionFactory=new ActiveMQConnectionFactory("tcp://localhost:61616");
            //2.创建连接对象
            connection=activeMQConnectionFactory.createConnection("admin","admin");
            connection.start();
            //3.创建会话
            session=connection.createSession(false,Session.CLIENT_ACKNOWLEDGE);
            //4.创建点对点连接目标
            Destination destination=session.createQueue("queue1");
            //创建订阅的目标
            Destination destination1=session.createTopic("topic1");

            //5.创建消费者消息
            consumer=session.createConsumer(destination);
            //6.接受消息
            Message message=consumer.receive();
            Message message1=consumer.receive();
           /* message.acknowledge();*/
            if(message instanceof TextMessage){
                System.out.println("收到消息1:"+((TextMessage) message).getText());
                System.out.println("收到消息2:"+((TextMessage) message1).getText());
            }else {
                System.out.println(message);
            }
        }catch (JMSException e){
            e.printStackTrace();
        }finally {
            if(consumer!=null){
                try{
                    session.close();
                }catch (JMSException e1){
                    e1.printStackTrace();
                }
            }
            if(connection!=null){
                try{
                    connection.close();
                }catch (JMSException e1){
                    e1.printStackTrace();
                }
            }
        }
    }

注:设置创建会话的类型时候,只有AUTO_ACKNOWLEDGE不需要确认,其余两个CLIENT_ACKNOWLEDGE和DUPS_OK_ACKNOWLEDGE需要在客户端调用acknowledge()方法。

Message message=consumer.receive();
message.acknowledge();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值