温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ActiveMQ中间消息件如何在Java项目中使用

发布时间:2021-03-23 15:28:32 来源:亿速云 阅读:204 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关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项目中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI