栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现消息队列的两种方式(小结)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现消息队列的两种方式(小结)

实现消息队列的两种方式

Apache ActiveMQ官方实例发送消息

直接在Apache官网http://activemq.apache.org/download-archives.html下载ActiveMQ源码

下载解压后拿到java代码实例

 

然后倒入IDE

如下:

 

请认真阅读readme.md文件,大致意思就是把项目打成两个jar包,然后启动服务,然后同时运行打的两个jar包,然后就能看到具体的调用信息。打jar包时直接利用maven打就行了,不用修改代码。

启动服务:

 

利用Spring消息模板发送消息

Spirng对Apache ActiveMQ提供了很好的支持

 

生成者代码:

package com.jms.service.impl;

import com.jms.service.ProducerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;


@Service
public class ProducerServiceImpl implements ProducerService {

 @Resource
 private JmsTemplate jmsTemplate;

 public void sendMessage(Destination destination, String msg) {
  System.out.println("向队列"+destination+"发送消息");
  jmsTemplate.convertAndSend(destination,msg);
 }

 public void sendMessage(String msg) {
  System.out.println("向队列"+jmsTemplate.getDefaultDestination().toString()+"发送消息");
  jmsTemplate.convertAndSend(msg);
 }
}

消费者代码:

package com.jms.service.impl;

import com.jms.service.CustomerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

@Service
public class CustomerServiceImpl implements CustomerService {

 @Resource
 private JmsTemplate jmsTemplate;
 
 public void receive(Destination destination) {

  TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
  try {
   System.out.println("从队列》"+destination.toString()+"成功获取消息》"+textMessage.getText());
  } catch (JMSException e) {
   e.printStackTrace();
  }

 }
}

Spring配置代码




 
  

 
 

 
 

 
 
  
  
 


 
 
  
 

 
 
  
  
   queue1
  
 

 
 
  
  
  
 

测试代码

package com.jsm.test;

import com.jms.service.CustomerService;
import com.jms.service.ProducerService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.jms.Destination;


public class JmsTest {


 @Test
 public void producerTest(){

  ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:spring-core.xml"});
  ProducerService producerService = (ProducerService)springContext.getBean("producerServiceImpl");
  CustomerService customerService = (CustomerService)springContext.getBean("customerServiceImpl");

  Destination destination = (Destination)springContext.getBean("queueDestination");
  producerService.sendMessage("测试消息队列");
  customerService.receive(destination);
 }
}

测试结果

 

代码地址

https://github.com/wahnn/SpringJMS
https://gitee.com/wahnn/SpringJMS

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139439.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号