ActiveMQ中间消息件如何在Java项目中使用
更新:HHH   时间:2023-1-8


今天就跟大家聊聊有关ActiveMQ中间消息件如何在Java项目中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

消息一共有两种接收和发送形式:点对点和发布定阅模式,也就是“一对一”和“一对多”。

1.导包(maven):

  <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-client</artifactId>
      <version>5.13.4</version>
    </dependency>

2.开始写类,提供者(发送者)和消费者(接收者)是两个不同的项目,我们先创建普通的maven项目,而不是web项目点对点的方式(消息只能被消费一次,如果同时有多个消费者,谁先抢到就是谁的)

消息提供者

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

    //创建连接工厂,这个参数就是自己的activeMQ的地址
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.180:61616");

    //2.创建连接
    Connection connection = connectionFactory.createConnection();

    //3.启动连接
    connection.start();

    //4.获取session(会话对象)
    /*
    arg0 是否启用事务
    arg1 消息的确认方式 自动确认
     */
    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

    //5.创建一个队列对象,名称
    Queue firstQueue = session.createQueue("firstQueue");

    //6.创建一个消息的生产者对象
//    Destination destination = ;//目标对象
    MessageProducer producer = session.createProducer(firstQueue);

    //7.创建一个消息
    TextMessage textMessage = session.createTextMessage("欢迎来到奇的天喻软件");

    //8.发送消息
    producer.send(textMessage);
    //9.关闭资源
    producer.close();
    session.close();
    connection.close();

  }

消息消费者

前几步是一样的,都是创建连接,只有第6步不一样,创建的是一个消费者

public static void main(String[] args) throws JMSException, IOException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.180:61616");

    //2.创建连接
    Connection connection = connectionFactory.createConnection();

    //3.启动连接
    connection.start();

    //4.获取session(会话对象)
    /*
    arg0 是否启用事务
    arg1 消息的确认方式 自动确认
     */
    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

    //5.创建一个队列对象,名称
    Queue firstQueue = session.createQueue("firstQueue");



    //6.创建消息消费者对象
    MessageConsumer consumer = session.createConsumer(firstQueue);

    //7.设置监听
    consumer.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage)message;
        try {
          System.out.println("提取的消息是"+textMessage.getText());
        } catch (JMSException e) {
          e.printStackTrace();
        }
      }
    });

    //8.等待键盘输入
    //目的是为了让程序停止来看效果
    System.in.read();


    //9.关闭资源
    consumer.close();
    session.close();
    connection.close();
  }

发布订阅模式(发布消息后,只有在之前运行的消费者才能收到,消息被任何一个消费者消费后,以后启动的消费者不能消费之前的消息)

消息提供者

 //创建连接工厂
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.180:61616");

    //2.创建连接
    Connection connection = connectionFactory.createConnection();

    //3.启动连接
    connection.start();

    //4.获取session(会话对象)
    /*
    arg0 是否启用事务
    arg1 消息的确认方式 自动确认
     */
    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

    //5
    Topic topic = session.createTopic("first-topic");


    //6.创建一个消息的生产者对象
//    Destination destination = ;//目标对象
    MessageProducer producer = session.createProducer(topic);

    //7.创建一个消息
    TextMessage textMessage = session.createTextMessage("欢迎来到奇的天喻软件");

    //8.发送消息
    producer.send(textMessage);


    //9.关闭资源
    producer.close();
    session.close();
    connection.close();

消费者

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.180:61616");

    //2.创建连接
    Connection connection = connectionFactory.createConnection();

    //3.启动连接
    connection.start();

    //4.获取session(会话对象)
    /*
    arg0 是否启用事务
    arg1 消息的确认方式 自动确认
     */
    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

    //5
    Topic topic = session.createTopic("first-topic");



    //6.创建消息消费者对象
    MessageConsumer consumer = session.createConsumer(topic);

    //7.设置监听
    consumer.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage)message;
        try {
          System.out.println("提取的消息是"+textMessage.getText());
        } catch (JMSException e) {
          e.printStackTrace();
        }
      }
    });
    //8.等待键盘输入
    //目的是为了让程序停止来看效果
    System.in.read();
    //9.关闭资源
    consumer.close();
    session.close();
    connection.close();

看完上述内容,你们对ActiveMQ中间消息件如何在Java项目中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。

返回编程语言教程...