为什么80%的码农都做不了架构师?>>>   

一、Broker配置

1、broker.properties文件内容:

broker.tcp.local.name=localTcpBroker

broker.tcp.local.url=tcp\://localhost\:61616?trace\=true&keepAlive\=true

broker.tcp.local.queue=Q.TCP.LOCAL

2、beans-tcp-broker.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:com/demo/broker.properties</value></list></property></bean><bean id="tcpConnector" class="org.apache.activemq.broker.TransportConnector"><property name="uri" value="${broker.tcp.local.url}"></property></bean><bean id="tcpQueue" class="org.apache.activemq.command.ActiveMQQueue"><property name="physicalName" value="${broker.tcp.local.queue}"></property></bean><bean id="kahaPersistenceAdapter"class="org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter"><property name="persistentIndex" value="true"></property><property name="maxDataFileLength" value="1048576"></property></bean><bean id="kahaDBPersistenceAdapter"class="org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter"><property name="checkForCorruptJournalFiles" value="true"></property><property name="checkpointInterval" value="5000"></property><property name="checksumJournalFiles" value="true"></property><property name="cleanupInterval" value="30000"></property><property name="directory" value="activemq-data"></property><property name="enableIndexWriteAsync" value="true"></property><property name="enableJournalDiskSyncs" value="true"></property><property name="ignoreMissingJournalfiles" value="false"></property><property name="indexCacheSize" value="100"></property><property name="journalMaxFileLength" value="1048576"></property><property name="journalMaxWriteBatchSize" value="1000"></property></bean><bean id="brokerService" class="org.apache.activemq.broker.BrokerService"init-method="start" destroy-method="stop" scope="singleton"><property name="brokerName" value="${broker.tcp.local.name}"></property><property name="useJmx" value="false"></property><property name="persistenceAdapter" ref="kahaPersistenceAdapter"></property><property name="transportConnectors"><list><ref local="tcpConnector" /></list></property><property name="destinations"><set><ref local="tcpQueue" /></set></property><property name="plugins"><set><ref local="loggingBrokerPlugin" /><ref local="destinationDotFilePlugin" /><ref local="statisticsBrokerPlugin" /></set></property></bean><bean id="loggingBrokerPlugin" class="org.apache.activemq.broker.util.LoggingBrokerPlugin"><property name="logAll" value="true"></property></bean><bean id="destinationDotFilePlugin"class="org.apache.activemq.broker.view.DestinationDotFilePlugin"><property name="file" value="ActiveMQDestinations.dot.txt"></property></bean><bean id="statisticsBrokerPlugin" class="org.apache.activemq.plugin.StatisticsBrokerPlugin" /></beans>

二、消息生产者配置

1、beans-tcp-producer.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:com/demo/broker.properties</value></list></property></bean><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="${broker.tcp.local.url}"></property></bean><!-- 采用TCP长连接方式, 避免每次建立短连接需要的额外工作时间 --><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><constructor-arg ref="connectionFactory"></constructor-arg></bean><bean id="tcpQueue" class="org.apache.activemq.command.ActiveMQQueue"><property name="physicalName" value="${broker.tcp.local.queue}"></property></bean><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory"><bean class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean></property><property name="messageConverter" ref="messageConverter" /><property name="sessionTransacted" value="true"></property></bean><!-- 消息转换 --><bean id="messageConverter" class="com.demo.client.MyMessageConverter" /><!-- 消息生产 --><bean id="messageProducer" class="com.demo.client.MyMessageProducer"><property name="template" ref="jmsTemplate" /><property name="destination" ref="tcpQueue" /></bean></beans>

2、消息转换类:

public class MyMessageConverter implements MessageConverter {private static Logger log = Logger.getLogger(MyMessageConverter.class);@SuppressWarnings("unchecked")public Object fromMessage(Message msg) throws JMSException,MessageConversionException {if (msg instanceof ObjectMessage) {HashMap<String, byte[]> map = (HashMap<String, byte[]>) ((ObjectMessage) msg).getObjectProperty("Map");try {ByteArrayInputStream bis = new ByteArrayInputStream(map.get("MSG_ID"));ObjectInputStream ois = new ObjectInputStream(bis);Object o = ois.readObject();ois.close();bis.close();return o;} catch (IOException e) {log.error("failed to read object message: " + e.getMessage());} catch (ClassNotFoundException e) {log.error("failed to read object message: " + e.getMessage());}} else {throw new JMSException("Message: [" + msg + "] is not a Map !");}return null;}public Message toMessage(Object obj, Session session) throws JMSException,MessageConversionException {if (obj instanceof MyMessage) {ActiveMQObjectMessage o = (ActiveMQObjectMessage) session.createObjectMessage();Map<String, byte[]> map = new HashMap<String, byte[]>();try {ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(bos);oos.writeObject(obj);map.put("MSG_ID", bos.toByteArray());oos.close();bos.close();} catch (IOException e) {log.error("failed to write object message: " + e.getMessage());}o.setObjectProperty("Map", map);return o;} else {throw new JMSException("Object: [" + obj + "] is not a Message !");}}}

3、消息生产者类:

public class MyMessageProducer {private static Logger log = Logger.getLogger(MyMessageProducer.class);private JmsTemplate template;private Queue destination;public void setTemplate(JmsTemplate template) {this.template = template;}public void setDestination(Queue destination) {this.destination = destination;}public void send(GrccMessage message) {this.template.convertAndSend(this.destination, message);log.info("生产消息 ==>\n" + message);try {Thread.sleep(1000);} catch (InterruptedException e) {}}}

三、消息消费者配置:

1、beans-tcp-consumer.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:com/demo/broker.properties</value></list></property></bean><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="${broker.tcp.local.url}"></property></bean><!-- 采用TCP长连接方式, 避免每次建立短连接需要的额外工作时间 --><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><constructor-arg ref="connectionFactory"></constructor-arg></bean><bean id="tcpQueue" class="org.apache.activemq.command.ActiveMQQueue"><property name="physicalName" value="${broker.tcp.local.queue}"></property></bean><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory"><bean class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean></property><property name="messageConverter" ref="messageConverter" /><property name="sessionTransacted" value="true"></property></bean><bean id="messageConverter" class="com.demo.client.MyMessageConverter" /><bean id="messageListener"class="org.springframework.jms.listener.adapter.MessageListenerAdapter"><constructor-arg><bean class="com.demo.client.MyMessageConsumer"></bean></constructor-arg><property name="defaultListenerMethod" value="consume" /><property name="messageConverter" ref="messageConverter" /></bean><bean id="listenerContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="tcpQueue" /><property name="messageListener" ref="messageListener" /></bean></beans>

2、消息消费者类:

public class MyMessageConsumer {private static Logger log = Logger.getLogger(MyMessageConsumer.class);public void consume(MyMessage message) {log.info("消费消息 <==\n" + message);
//      try {
//          Thread.sleep(1000);
//      } catch (InterruptedException e) {
//      }}}

转载于:https://my.oschina.net/qingc/blog/10959

Spring+ActiveMQ配置相关推荐

  1. 使用spring + ActiveMQ 总结

    使用spring + ActiveMQ 总结 摘要 Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 目录[-] Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 ...

  2. Spring ActiveMQ示例(第2部分)

    This is the second part in the Spring ActiveMQ example tutorial. Please head over to the first part ...

  3. Spring Boot配置属性(567个,丁雪峰 译 《Spring Boot 实战》 附录)

    flyway flyway.baseline 执行基线时标记已有Schema的描述 flyway.baseline-on-migrate 在没有元数据表的情况下,针对非空 Schema执行迁移时是否自 ...

  4. ActiveMQ 配置启动文件介绍

    1.配置jdk环境(Linux) 一般在普通用户下的 ~/.bash_profile 或者在 /etc/profile 最后添加如下语句: export JAVA_HOME=/opt/app/jdk1 ...

  5. spring boot配置详情

    spring boot配置详情如下:  1.MVC相关  mvc  spring.mvc.async.request-timeout设定async请求的超时时间,以毫秒为单位,如果没有设置的话,以具体 ...

  6. Spring中配置DataSource数据源的几种选择

    Spring中配置DataSource数据源的几种选择 在Spring框架中有如下3种获得DataSource对象的方法: 从JNDI获得DataSource. 从第三方的连接池获得DataSourc ...

  7. java避免使用orderby_java – Spring安全配置@Order不是唯一的例外

    我试图在我的Spring Security配置中注册多个过滤器,但是我总是得到相同的异常: 04-Nov-2015 14:35:23.792 WARNING [RMI TCP Connection(3 ...

  8. Spring AOP源码分析(六)Spring AOP配置的背后

    本篇文章主要对Spring AOP配置背后进行了哪些事情做下说明.还是如上类似的工程,在xml中AOP拦截配置如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 < ...

  9. Spring 数据源配置与应用

    Spring 数据源配置与应用 Spring对数据库操作都依赖数据源. Spring有默认的数据源实现org.springframework.jdbc.datasource.DriverManager ...

最新文章

  1. 3d镜头 适配_您是否应该将镜头适配器与无反光镜相机一起使用?
  2. Flutter TextField 设置默认值和光标位置
  3. 怎么查redis 中的 cache_20、springcloud如何使用spring-cache
  4. HOHO 拿了個小嘉獎 Happy一下 可惜過年可能被留下...痛苦
  5. CF932E Team Work(第二类斯特林数)
  6. Mac下最好用的离线词典-欧陆词典破解版
  7. java技术管理的简历_基于javaweb个人简历生成及管理系统.doc
  8. Word 2010 从任意页码重新开始
  9. 基于Kinect Azure的多相机数据采集(一)
  10. 智能点餐mysql框架图_SpringBoot微信点餐系统--P3数据库设计
  11. 苹果电脑怎么删除移动硬盘里的文件,苹果电脑无法删除移动硬盘文件
  12. 机器学习——支持向量机——硬间隔与支持向量
  13. 无向图边数和顶点关系_离散数学中的二元关系
  14. Android——一个简单的智能家居系统
  15. Vue-V-model参数绑定
  16. 用windows“记事本”创建一个文本文件(hamlet.txt),其中每行包含一段英文。试读出文件的全部内容,并判断:(1)该文本文件有多少行?(2)文件中以大写字母开头的有多少行?
  17. 全自动抠图换背景软件下载_手机一键抠图换背景,用这个APP就是这么简单
  18. 10款自媒体人必备的免费工具,快速高效运营
  19. wordpress最佳架构_应用程序的21个最佳WordPress主题(2020)
  20. 【C语言】青蛙跳台阶问题

热门文章

  1. JS监听手机物理返回键,返回到指定页面
  2. (原創) 如何解決移除DSP Builder後,在Matlab殘留錯誤訊息的問題? (SOC) (DSP Builder) (Matlab)...
  3. 从源码角度来读Handler
  4. 如何关注掘金的所有小伙伴
  5. 使用CSS如何解决inline-block元素的空白间距
  6. [zz]kvm环境快照(snapshot)的使用方法
  7. 在 Linux“.NET研究” 操作系统中运行 ASP.NET 4 (下)
  8. 程序员的比较工具大杂烩
  9. #define、#undef、#ifdef、#ifndef、#if、#elif、#else、#endif、defined解释
  10. bzoj4568(合并线性基+倍增)