当前位置:   article > 正文

rabbitmq集成spring的xml配置和java代码_xml配置中rabbit如何换成java配置

xml配置中rabbit如何换成java配置

RabbitMQ官方支持以下五种队列类型。

队列名称队列模型适用
简单队列
工作队列
发布/订阅模式
路由模式
主题模式

 

使用依赖:

  1. <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
  2. <dependency>
  3. <groupId>org.springframework.amqp</groupId>
  4. <artifactId>spring-rabbit</artifactId>
  5. <version>1.7.12.RELEASE</version>
  6. </dependency>

xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  10. http://www.springframework.org/schema/rabbit
  11. http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">
  12. <context:property-placeholder location="classpath:config.properties"/>
  13. <!--配置connection-factory,指定连接rabbit server参数 -->
  14. <rabbit:connection-factory id="connectionFactory" host="${rabbit.mq.host}" username="${rabbit.mq.username}"
  15. password="${rabbit.mq.password}" port="${rabbit.mq.port}"/>
  16. <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
  17. <rabbit:admin id="connectAdmin" connection-factory="connectionFactory"/>
  18. <!-- 消息消费者 begin-->
  19. <!-- 定义消费者实现类 begin-->
  20. <bean id="mdmDataHandleService" class="com.fsl.lcp.mq.service.impl.MdmDataHandleServiceImpl"/>
  21. <!-- 定义消费者实现类 end -->
  22. <!-- 定义消费者,并根据消息类型,绑定实现类 -->
  23. <bean id="rabbitMqReceiver" class="com.fsl.lcp.mq.listener.RabbitMqReceiver">
  24. <property name="serviceBeanMap">
  25. <map>
  26. <entry key="test" value-ref="mdmDataHandleService"/>
  27. </map>
  28. </property>
  29. </bean>
  30. <!--定义queue -->
  31. <rabbit:queue name="${rabbit.mq.queue}" durable="true" auto-delete="false" exclusive="false"
  32. declared-by="connectAdmin"/>
  33. <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
  34. <rabbit:listener-container connection-factory="connectionFactory">
  35. <rabbit:listener queues="${rabbit.mq.queue}" ref="rabbitMqReceiver"/>
  36. </rabbit:listener-container>
  37. <!-- 消息消费者 end-->
  38. <!-- 消息生产者 begin-->
  39. <bean class="com.fsl.lcp.mq.producer.RabbitMqProducer"></bean>
  40. <!--定义rabbit template用于数据的发送 -->
  41. <rabbit:template id="rabbitMqTemplate" connection-factory="connectionFactory"/>
  42. <!-- 消息生产者 end-->
  43. </beans>

config.properties配置:

  1. #rabbitmq rabbitmq.open-->Y/N
  2. rabbitmq.open=Y
  3. rabbit.mq.host=172.16.20.121
  4. rabbit.mq.port=5672
  5. rabbit.mq.username=fsl
  6. rabbit.mq.password=fsl
  7. rabbit.mq.queue=queue.portal.default

java代码--mq接收者--RabbitMqReceiver.java

  1. import java.util.Map;
  2. import com.fsl.lcp.mq.service.MqHandleService;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.amqp.core.Message;
  6. import org.springframework.amqp.core.MessageListener;
  7. /**
  8. * 〈rabbitmq的消费者〉.
  9. *
  10. * @author tanqian
  11. * @create 2019/1/2
  12. */
  13. public class RabbitMqReceiver implements MessageListener {
  14. private Logger log = LoggerFactory.getLogger(RabbitMqReceiver.class);
  15. private Map<String, MqHandleService> serviceBeanMap;
  16. @Override
  17. public void onMessage(Message message) {
  18. try {
  19. String str = new String(message.getBody(), "utf-8");
  20. log.info(" receive body------->:" + str);
  21. //处理消息
  22. String type = "test";
  23. serviceBeanMap.get(type).handleMqData(message);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. log.error("error:" + e.getMessage());
  27. }
  28. }
  29. public Map<String, MqHandleService> getServiceBeanMap() {
  30. return serviceBeanMap;
  31. }
  32. public void setServiceBeanMap(Map<String, MqHandleService> serviceBeanMap) {
  33. this.serviceBeanMap = serviceBeanMap;
  34. }
  35. }

java代码--mq消息处理者公共接口类--MqHandleService.java

  1. import org.springframework.amqp.core.Message;
  2. /**
  3. * 处理mq消息的接口类.
  4. *
  5. * @author tanqian
  6. * @date 2019/01/11
  7. */
  8. public interface MqHandleService {
  9. /**
  10. * 处理mq过来的数据.
  11. */
  12. void handleMqData(Message message);
  13. }

java代码--mq消息处理者实现类--MdmDataHandleServiceImpl.java

  1. import com.fsl.lcp.mq.service.MqHandleService;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.amqp.core.Message;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.transaction.annotation.Transactional;
  7. /**
  8. * 处理mdm的mq消息的实现类.
  9. *
  10. * @author tanqian
  11. * @date 2019/01/07
  12. */
  13. @Transactional(rollbackFor = Exception.class)
  14. public class MdmDataHandleServiceImpl implements MqHandleService {
  15. private Logger log = LoggerFactory.getLogger(MdmDataHandleServiceImpl.class);
  16. @Override
  17. public void handleMqData(Message message) {
  18. try {
  19. //处理消息
  20. String str = new String(message.getBody(), "utf-8");
  21. log.info(" receive body------->:" + str);
  22. } catch (Exception e) {
  23. log.error("Exception------->:" + e.getMessage());
  24. }
  25. }
  26. }

java代码--mq消息发送者类--RabbitMqProducer.java,其中rabbitMqTemplate可以直接通过注解获取

  1. import java.io.IOException;
  2. import com.hand.hap.core.components.ApplicationContextHelper;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.amqp.core.AmqpTemplate;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.ApplicationContext;
  8. /**
  9. * rabbitmq消息的生产者.
  10. *
  11. * @author tanqian
  12. * @date 2019/01/11
  13. */
  14. public class RabbitMqProducer {
  15. private Logger log = LoggerFactory.getLogger(RabbitMqProducer.class);
  16. private ApplicationContext beanFactory;
  17. @Value("${rabbitmq.open}")
  18. private String rabbitmqOpen;
  19. // @Resource(name = "rabbitMqTemplate")
  20. private AmqpTemplate rabbitMqTemplate;
  21. public void sendMessage(String exchange, String routingKey, Object message) throws IOException {
  22. //初始化bean
  23. this.loadService();
  24. log.info("to send message:{}", message);
  25. rabbitMqTemplate.convertAndSend(exchange, routingKey, message);
  26. }
  27. /**
  28. * 加载注入的bean.
  29. */
  30. private void loadService() {
  31. if (null == beanFactory) {
  32. beanFactory = ApplicationContextHelper.getApplicationContext();
  33. if (null == beanFactory) {
  34. return;
  35. }
  36. }
  37. if (rabbitMqTemplate == null && rabbitmqOpen.equals("Y")) {
  38. rabbitMqTemplate = (AmqpTemplate) beanFactory.getBean("rabbitMqTemplate");
  39. }
  40. }
  41. }

文档参考:

中文文档:http://rabbitmq.mr-ping.com/

官方文档:http://www.rabbitmq.com/getstarted.html

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/139202
推荐阅读
相关标签
  

闽ICP备14008679号