设为首页 加入收藏

TOP

Java连接RabbitMQ实例(四)
2019-08-04 00:14:07 】 浏览:185
Tags:Java 连接 RabbitMQ 实例
= environment.getProperty("spring.rabbitmq.virtual-host");
 CachingConnectionFactory connectionFactory = new CachingConnectionFactory(addresses, port);
 connectionFactory.setUsername(username);
 connectionFactory.setPassword(password);
 connectionFactory.setVirtualHost(virtualHost);
 connectionFactory.setPublisherConfirms(true);
 return connectionFactory;
 }


 @Bean
 public FanoutExchange fanoutExchange() {
 return new FanoutExchange(FANOUT_EXCHANGE, true, false);
 }


 @Bean
 public DirectExchange directExchange() {
 return new DirectExchange(DIRECT_EXCHANGE, true, false);
 }


 @Bean
 public TopicExchange topicExchange() {
 return new TopicExchange(TOPIC_EXCHANGE, true, false);
 }


 @Bean
 public Queue fanoutQueue() {
 return new Queue(FANOUT_QUEUE, true);
 }


 @Bean
 public Queue directQueue() {
 return new Queue(DIRECT_QUEUE, true);
 }


 @Bean
 public Queue topicQueue() {
 return new Queue(TOPIC_QUEUE, true);
 }


 @Bean
 public Binding fanoutBinding() {
 return BindingBuilder.bind(fanoutQueue()).to(fanoutExchange());
 }


 @Bean
 public Binding directBinding() {
 return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTINGKEY);
 }


 @Bean
 public Binding topicBinding() {
 return BindingBuilder.bind(topicQueue()).to(topicExchange()).with(TOPIC_ROUTINGKEY);
 }
}
2.4 生产者代码


import java.util.Map;
import java.util.UUID;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;


import com.hys.springboot.entity.Order;
import com.hys.springboot.service.IRabbitSenderService;


@Service
public class RabbitSenderServiceImpl implements IRabbitSenderService {


 private static final Log logger = LogFactory.getLog(RabbitSenderServiceImpl.class);
 @Autowired
 private RabbitTemplate rabbitTemplate;
 final ConfirmCallback CONFIRM_CALLBACK = (correlationData, ack, cause) -> {
 if (logger.isDebugEnabled()) {
 logger.debug("correlationData:" + correlationData + " ack:" + ack);
 }
 if (!ack) {
 if (logger.isErrorEnabled()) {
 logger.error("异常处理");
 }
 }
 };
final ReturnCallback RETURN_CALLBACK = (message, replyCode, replyText, exchange, routingKey) -> {
if (logger.isErrorEnabled()) {
 logger.error("replyCode:" + replyCode + " replyText:" + replyText + " exchange:" + exchange + " routingKey:" + routingKey);
 }
 };


 @Override
 

首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PHPH实现水仙花数的5个示例 下一篇Java class 文件简介

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目