欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)消息隊(duì)列的兩種方式(小結(jié))

 更新時(shí)間:2018年12月12日 15:24:20   作者:朽木要自雕  
本文主要介紹了兩種java實(shí)現(xiàn)消息隊(duì)列的方式,利用Spring消息模板發(fā)送消息和Apache ActiveMQ官方實(shí)例發(fā)送消息,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

實(shí)現(xiàn)消息隊(duì)列的兩種方式

Apache ActiveMQ官方實(shí)例發(fā)送消息

直接在Apache官網(wǎng)http://activemq.apache.org/download-archives.html下載ActiveMQ源碼

下載解壓后拿到j(luò)ava代碼實(shí)例

 

然后倒入IDE

如下:

 

請(qǐng)認(rèn)真閱讀readme.md文件,大致意思就是把項(xiàng)目打成兩個(gè)jar包,然后啟動(dòng)服務(wù),然后同時(shí)運(yùn)行打的兩個(gè)jar包,然后就能看到具體的調(diào)用信息。打jar包時(shí)直接利用maven打就行了,不用修改代碼。

啟動(dòng)服務(wù):

 

利用Spring消息模板發(fā)送消息

Spirng對(duì)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;

/**
 * 發(fā)送消息
 */
@Service
public class ProducerServiceImpl implements ProducerService {

 @Resource
 private JmsTemplate jmsTemplate;

 public void sendMessage(Destination destination, String msg) {
  System.out.println("向隊(duì)列"+destination+"發(fā)送消息");
  jmsTemplate.convertAndSend(destination,msg);
 }

 public void sendMessage(String msg) {
  System.out.println("向隊(duì)列"+jmsTemplate.getDefaultDestination().toString()+"發(fā)送消息");
  jmsTemplate.convertAndSend(msg);
 }
}

消費(fèi)者代碼:

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("從隊(duì)列》"+destination.toString()+"成功獲取消息》"+textMessage.getText());
  } catch (JMSException e) {
   e.printStackTrace();
  }

 }
}

Spring配置代碼

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd
  ">

 <!-- 啟動(dòng)包掃描功能,以便注冊(cè)帶有@Controller、@Service、@repository、@Component等注解的類成為spring的bean -->
 <context:component-scan base-package="com.jms.service"> </context:component-scan>

 <!-- 配置根視圖 -->
 <!--<mvc:view-controller path="/" view-name="index"/>-->

 <!--啟用注解-->
 <mvc:annotation-driven />

 <!-- 視圖層配置 -->
 <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
  <!--<property name="prefix" value="/WEB-INF/view/"/>-->
  <!--<property name="suffix" value=".jsp"/>-->
 <!--</bean>-->


 <!-- 配置JMS連接工廠 -->
 <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  <property name="brokerURL" value="tcp://localhost:61616" />
 </bean>

 <!-- 定義消息隊(duì)列(Queue) -->
 <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  <!-- 設(shè)置消息隊(duì)列的名字 -->
  <constructor-arg>
   <value>queue1</value>
  </constructor-arg>
 </bean>

 <!-- 配置JMS模板(Queue),Spring提供的JMS工具類,它發(fā)送、接收消息。 -->
 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="defaultDestination" ref="queueDestination" />
  <property name="receiveTimeout" value="10000" />
 </bean>
</beans>

測(cè)試代碼

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;

/**
 * 消息隊(duì)列測(cè)試類
 */
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("測(cè)試消息隊(duì)列");
  customerService.receive(destination);
 }
}

測(cè)試結(jié)果

 

代碼地址

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringMVC配置404踩坑記錄

    SpringMVC配置404踩坑記錄

    本文主要介紹了SpringMVC配置404踩坑記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • Spring Boot web項(xiàng)目的TDD流程

    Spring Boot web項(xiàng)目的TDD流程

    TDD(Test-driven development) 測(cè)試驅(qū)動(dòng)開(kāi)發(fā),簡(jiǎn)單點(diǎn)說(shuō)就是編寫測(cè)試,再編寫代碼。這是首要一條,不可動(dòng)搖的一條,先寫代碼后寫測(cè)試的都是假TDD。
    2021-05-05
  • 詳解Java編程中throw和throws子句的使用方法

    詳解Java編程中throw和throws子句的使用方法

    這篇文章主要介紹了詳解Java編程中throw和throws子句的使用方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • Java第三方庫(kù)JodaTime的具體使用

    Java第三方庫(kù)JodaTime的具體使用

    本文主要介紹了Java第三方庫(kù)JodaTime的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring Boot支持HTTPS步驟詳解

    Spring Boot支持HTTPS步驟詳解

    這篇文章主要介紹了Spring Boot支持HTTPS步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 分析并發(fā)編程之LongAdder原理

    分析并發(fā)編程之LongAdder原理

    LongAdder類是JDK1.8新增的一個(gè)原子性操作類。AtomicLong通過(guò)CAS算法提供了非阻塞的原子性操作,相比受用阻塞算法的同步器來(lái)說(shuō)性能已經(jīng)很好了,但是JDK開(kāi)發(fā)組并不滿足于此,因?yàn)榉浅8悴l(fā)的請(qǐng)求下AtomicLong的性能是不能讓人接受的
    2021-06-06
  • JAVA使用Ldap操作AD域的方法示例

    JAVA使用Ldap操作AD域的方法示例

    這篇文章主要介紹了JAVA使用Ldap操作AD域的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Springboot導(dǎo)入本地jar后 打包依賴無(wú)法加入的解決方案

    Springboot導(dǎo)入本地jar后 打包依賴無(wú)法加入的解決方案

    這篇文章主要介紹了Springboot導(dǎo)入本地jar后 打包依賴無(wú)法加入的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java同步函數(shù)代碼詳解

    Java同步函數(shù)代碼詳解

    這篇文章主要介紹了Java線程中的同步函數(shù)的相關(guān)內(nèi)容,涉及了實(shí)例代碼,需要的朋友,可以參考下。
    2017-10-10
  • SpringBoot實(shí)現(xiàn)掃碼登錄的項(xiàng)目實(shí)踐

    SpringBoot實(shí)現(xiàn)掃碼登錄的項(xiàng)目實(shí)踐

    本文主要介紹了SpringBoot實(shí)現(xiàn)掃碼登錄的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論