ActiveMQConfig.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.yihu.wlyy.config;
  2. import com.yihu.wlyy.activemq.HealthArtListener;
  3. import com.yihu.wlyy.util.SpringUtil;
  4. import org.apache.activemq.ActiveMQConnectionFactory;
  5. import org.apache.activemq.ActiveMQSession;
  6. import org.apache.activemq.command.ActiveMQQueue;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Primary;
  13. import org.springframework.jms.annotation.EnableJms;
  14. import org.springframework.jms.connection.CachingConnectionFactory;
  15. import org.springframework.jms.core.JmsTemplate;
  16. import org.springframework.jms.listener.DefaultMessageListenerContainer;
  17. import javax.annotation.PostConstruct;
  18. import javax.jms.MessageListener;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * Created by chenweida on 2017/9/9.
  23. * 生产者配置
  24. */
  25. @EnableJms
  26. @Configuration
  27. public class ActiveMQConfig {
  28. @Value("${activemq.username}")
  29. private String username;
  30. @Value("${activemq.password}")
  31. private String password;
  32. @Value("${activemq.url}")
  33. private String url;
  34. @Bean
  35. public ActiveMQConnectionFactory activeMQConnectionFactory() {
  36. ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(username, password, url);
  37. //设置异步发送
  38. activeMQConnectionFactory.setUseAsyncSend(true);
  39. return activeMQConnectionFactory;
  40. }
  41. /**
  42. * 缓存session链接
  43. *
  44. * @return
  45. */
  46. @Bean
  47. @Primary
  48. public CachingConnectionFactory CachingConnectionFactory() {
  49. CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
  50. //目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory
  51. cachingConnectionFactory.setTargetConnectionFactory(activeMQConnectionFactory());
  52. //Session缓存数量,这里属性也可以直接在这里配置
  53. cachingConnectionFactory.setSessionCacheSize(100);
  54. return cachingConnectionFactory;
  55. }
  56. @Bean
  57. public JmsTemplate jmsTemplate() {
  58. JmsTemplate jmsTemplate = new JmsTemplate();
  59. jmsTemplate.setConnectionFactory(CachingConnectionFactory());
  60. return jmsTemplate;
  61. }
  62. }