亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring-boot JMS 發(fā)送消息慢的解決方法

 更新時間:2017年08月04日 08:39:19   作者:YSHY  
這篇文章主要為大家詳細(xì)介紹了Spring-boot JMS 發(fā)送消息慢的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Spring-boot JMS 發(fā)送消息慢的問題解決

1、在《ActiveMQ 基于zookeeper的主從(levelDB Master/Slave)搭建以及Spring-boot下使用》中,采用以下代碼進行JMS消息發(fā)送:

@Service
public class Producer {

 @Autowired
 private JmsMessagingTemplate jmsTemplate;

 public void sendMessage(Destination destination, final String message){
  jmsTemplate.convertAndSend(destination, message);
 }
}

經(jīng)使用JMeter進行壓力測試,發(fā)現(xiàn)JMS的發(fā)送消息特別慢。

2、下面通過自定義CachingConnectionFactory解決。

(1)SenderConfig.java

package com.example.springbootactivemq.jms;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * Created by yan on 2017/8/3.
 */
@Configuration
public class SenderConfig {

 @Value("${spring.activemq.broker-url}")
 private String brokerUrl;

 @Bean
 public ActiveMQConnectionFactory activeMQConnectionFactory() {
  ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
  activeMQConnectionFactory.setBrokerURL(brokerUrl);

  return activeMQConnectionFactory;
 }

 @Bean
 public CachingConnectionFactory cachingConnectionFactory() {
  return new CachingConnectionFactory(activeMQConnectionFactory());
 }

 @Bean
 public JmsTemplate jmsTemplate() {
  return new JmsTemplate(cachingConnectionFactory());
 }

 @Bean
 public Sender sender() {
  return new Sender();
 }
}

(2)Sender.java

package com.example.springbootactivemq.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created by yan on 2017/8/3.
 */
public class Sender {

 @Autowired
 private JmsTemplate jmsTemplate;

 public void send(final String destination, final String message){
  this.jmsTemplate.convertAndSend(destination, message);
 }
}

(3)Receiver.java

package com.example.springbootactivemq.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.jms.support.JmsUtils;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created by yan on 2017/8/3.
 */
public class Receiver implements SessionAwareMessageListener<TextMessage> {

 @JmsListener(destination = "${queue.destination}")
 public void receive(String message) {
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }

 }
}

(4)ReceiverConfig.java

package com.example.springbootactivemq.jms;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;

/**
 * Created by yan on 2017/8/3.
 */
@Configuration
@EnableJms
public class ReceiverConfig {
 @Value("${spring.activemq.broker-url}")
 private String brokerUrl;

 @Bean
 public ActiveMQConnectionFactory activeMQConnectionFactory() {
  ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
  activeMQConnectionFactory.setBrokerURL(brokerUrl);

  return activeMQConnectionFactory;
 }

 @Bean
 public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
  DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
  factory.setConnectionFactory(activeMQConnectionFactory());
  factory.setConcurrency("3-10");

  return factory;
 }

 @Bean
 public Receiver receiver() {
  return new Receiver();
 }
}

(5)TestCtrl.java

package com.example.springbootactivemq.test;

import com.example.springbootactivemq.jms.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yan on 2017/8/2.
 */
@RestController
@RequestMapping(
  value = "/test",
  headers = "Accept=application/json",
  produces = "application/json;charset=utf-8"
)
public class TestCtrl {
 @Autowired
 private Sender sender;

 @Value("${queue.destination}")
 private String destination;

 @RequestMapping(
   value = "/say/{msg}/to/{name}",
   method = RequestMethod.GET
 )
 public Map<String, Object> say(@PathVariable String msg, @PathVariable String name){
  Map<String, Object> map = new HashMap<>();
  map.put("msg", msg);
  map.put("name", name);

  sender.send(destination, msg);

  return map;
 }
}

(6)application.properties

spring.activemq.broker-url=failover:(tcp://192.168.3.10:61616,tcp://192.168.3.11:61616,tcp://192.168.3.12:61616)
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.user=admin
spring.activemq.password=admin

queue.destination=test.queue
queue.concurrency=3-10

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

相關(guān)文章

  • 一文掌握J(rèn)ava開發(fā)工具Maven(簡單上手)

    一文掌握J(rèn)ava開發(fā)工具Maven(簡單上手)

    掌握maven的相關(guān)知識是Java開發(fā)必備的技能,今天通過本文從入門安裝開始,逐步深入講解maven的相關(guān)知識,包括maven的安裝到簡單上手maven項目開發(fā),感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • 解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題

    解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題

    這篇文章主要介紹了解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • mybatisplus邏輯刪除基本實現(xiàn)和坑點解決

    mybatisplus邏輯刪除基本實現(xiàn)和坑點解決

    這篇文章主要介紹了mybatisplus邏輯刪除基本實現(xiàn)和坑點解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • jdk1.8的環(huán)境配置過程

    jdk1.8的環(huán)境配置過程

    Java平臺由Java虛擬機和Java應(yīng)用程序接口搭建,Java語言則是進入這個平臺的通道,用Java語言編寫并編譯的程序可以運行在這個平臺上,本文給大家講解jdk1.8的環(huán)境配置過程,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • Java集合框架LinkedList詳解及實例

    Java集合框架LinkedList詳解及實例

    這篇文章主要介紹了Java集合框架LinkedList詳解及實例的相關(guān)資料,從定義,概述,用法進行介紹,需要的朋友可以參考下
    2017-04-04
  • Java判斷字符串為空、字符串是否為數(shù)字

    Java判斷字符串為空、字符串是否為數(shù)字

    這篇文章主要介紹了Java判斷字符串為空、字符串是否為數(shù)字,其中數(shù)字的判斷介紹了3種方法,需要的朋友可以參考下
    2014-06-06
  • form表單回寫技術(shù)java實現(xiàn)

    form表單回寫技術(shù)java實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)form表單回寫技術(shù)的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • Spring中的@Autowired注解深入解析與實戰(zhàn)指南

    Spring中的@Autowired注解深入解析與實戰(zhàn)指南

    本文介紹了Spring框架中的@Autowired注解,詳細(xì)講解了其基本用法、高級用法以及實際應(yīng)用場景,通過@Autowired注解,Spring容器可以自動將依賴的Bean注入到目標(biāo)Bean中,從而簡化代碼并提高可維護性,需要的朋友可以參考下
    2024-11-11
  • Springboot啟動執(zhí)行特定代碼的方式匯總

    Springboot啟動執(zhí)行特定代碼的方式匯總

    這篇文章主要介紹了Springboot啟動執(zhí)行特定代碼的幾種方式,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • java poi讀取excel操作示例(2個代碼)

    java poi讀取excel操作示例(2個代碼)

    這篇文章主要介紹了使用POI讀取EXCEL文件的方法,代碼大家可以參考使用
    2013-12-12

最新評論