activemq整合springboot使用方法(个人微信小程序用)
主题ActiveMQSpringBoot小程序开发
1.引入依赖
org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE org.springframework.boot spring-boot-starter-activemq
2.修改配置文件(其实配置也是默认值,不配置也可以)
spring.activemq.in-memory=true spring.activemq.pool.enabled=false
3.添加ActiveMq连接池(如果不开启连接池,则每发送一条数据创建一个连接)
①.添加依赖
org.apache.activemq activemq-pool
②.修改配置文件
#服务端口,8080被另一服务占用 server.port=9090 spring.activemq.broker-url=tcp://127.0.0.1:61616 #在考虑结束之前等待的时间 #spring.activemq.close-timeout=15s #默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。 spring.activemq.in-memory=true #是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。 spring.activemq.non-blocking-redelivery=false #等待消息发送响应的时间。设置为0等待永远。 spring.activemq.send-timeout=0 #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置 spring.jms.pub-sub-domain=true #账号 spring.activemq.user=admin #密码 spring.activemq.password=admin #是否信任所有包 #spring.activemq.packages.trust-all= #要信任的特定包的逗号分隔列表(当不信任所有包时) #spring.activemq.packages.trusted= #当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。 #spring.activemq.pool.block-if-full=true #如果池仍然满,则在抛出异常前阻塞时间。 #spring.activemq.pool.block-if-full-timeout=-1ms #是否在启动时创建连接。可以在启动时用于加热池。 #spring.activemq.pool.create-connection-on-startup=true #是否用Pooledconnectionfactory代替普通的ConnectionFactory。 #spring.activemq.pool.enabled=false #连接过期超时。 #spring.activemq.pool.expiry-timeout=0ms #连接空闲超时 #spring.activemq.pool.idle-timeout=30s #连接池最大连接数 #spring.activemq.pool.max-connections=1 #每个连接的有效会话的最大数目。 #spring.activemq.pool.maximum-active-session-per-connection=500 #当有"JMSException"时尝试重新连接 #spring.activemq.pool.reconnect-on-exception=true #在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。 #spring.activemq.pool.time-between-expiration-check=-1ms #是否只使用一个MessageProducer #spring.activemq.pool.use-anonymous-producers=true
4.添加JMS相关配置
①.开启JMS扫描注解:@EnableJms相当于application.xml中的
②.配置Queue类:
@Bean publicQueuequeue(){ returnnewActiveMQQueue("queueName1"); }
③.创建生产者:
@Resource JmsMessagingTemplatejmsMessagingTemplate; publicvoidsendMessage(Destinationdestination,Stringmessage){ jmsMessagingTemplate.convertAndSend(destination,message); }
④.创建消费者:
@JmsListener(destination="queueName1") publicvoidreceiveQueue(Stringmessage){ log.info("=========接受到了消息:"+message); grabService.addSearchContent(message,mainConfig.getCharset()); }
ps:@JmsListener(destination="queueName1")注解用于监听指定名称的消息
参数message代表具体的消息
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。