Spring-boot JMS 发送消息慢的解决方法
程序员文章站
2024-02-18 11:43:16
spring-boot jms 发送消息慢的问题解决
1、在《activemq 基于zookeeper的主从(leveldb master/slave)搭建以及spr...
spring-boot jms 发送消息慢的问题解决
1、在《activemq 基于zookeeper的主从(leveldb master/slave)搭建以及spring-boot下使用》中,采用以下代码进行jms消息发送:
@service public class producer { @autowired private jmsmessagingtemplate jmstemplate; public void sendmessage(destination destination, final string message){ jmstemplate.convertandsend(destination, message); } }
经使用jmeter进行压力测试,发现jms的发送消息特别慢。
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。