网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

实现消息队列的两种方式

主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站、程序开发、微网站、微信小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都做网站、成都网站制作、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体,具备承接不同规模与类型的建设项目的能力。

Apache ActiveMQ官方实例发送消息

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

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

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

然后倒入IDE

如下:

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

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

启动服务:

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

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

利用Spring消息模板发送消息

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

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

生成者代码:

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;
 /**
  * 接收消息
  * @param destination
  */
 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配置代码

<?xml version="1.0" encoding="UTF-8"?>


 
  

 
 

 
 

 
 
  
  
 


 
 
  
 

 
 
  
  
   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);
 }
}

测试结果

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

代码地址

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前标题:java实现消息队列的两种方式(小结)
本文URL:http://bjjierui.cn/article/ppjecj.html

其他资讯