http://tangxininjava.iteye.com/blog/2276211
在上面这篇基础上 使用rabbitmq 当然首先需要安装rabbitmq。关于rabbitmq的安装就不说了。
我安装的windows版本的 安装好了之后访问地址:http://localhost:15672/#/
默认登录用户名:guest 密码:guest
因为使用的spring boot 代码如下:
Application.java
package com.zmall; import com.rabbitmq.client.Channel; import com.zmall.service.Receiver; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.Connection; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.MultipartConfigFactory; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import javax.servlet.MultipartConfigElement; import javax.sql.DataSource; @Configuration @EnableAutoConfiguration @MapperScan(basePackages = "com.zmall.mapper") @ComponentScan(basePackages = "com.zmall") public class Application extends SpringBootServletInitializer { final static String queueName = "o2o.zmall.goods"; @Bean Queue queue() { return new Queue(queueName, false); } @Bean TopicExchange exchange() { return new TopicExchange("o2o.zmall"); } @Bean Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(queueName); } @Bean SimpleMessageListenerContainer container(MessageListenerAdapter listenerAdapter) { com.rabbitmq.client.ConnectionFactory client = new com.rabbitmq.client.ConnectionFactory(); client.setHost("localhost"); client.setPort(5672); ConnectionFactory connectionFactory = new CachingConnectionFactory(client); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); return container; } @Bean Receiver receiver() { return new Receiver(); } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setMaxFileSize("5MB"); factory.setMaxRequestSize("5MB"); return factory.createMultipartConfig(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); return sessionFactory.getObject(); } }
然后控制器中发送信息 IndexController.java
package com.zmall.controller; import com.zmall.model.City; import com.zmall.service.CityService; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by Administrator on 2016/2/10. */ @Controller @RequestMapping(value = "/index") public class IndexController { @Autowired RabbitTemplate rabbitTemplate; @Autowired private CityService cityService; @RequestMapping(value = "/get",method = RequestMethod.GET) @ResponseBody public String index(){ City city = cityService.getCity(1L); rabbitTemplate.convertAndSend("o2o.zmall.goods","Hello from RabbitMQ!"); return city.getName(); } }
在上面的Application.java中我们可以看到这个代码:
@Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); }
这个Receiver就是我们自己手动创建的 代码如下 Receiver.java
package com.zmall.service; /** * Created by Administrator on 2016/2/12. */ public class Receiver { public void receiveMessage(String message) { System.out.println("Received <" + message + ">"); } }
启动tomcat 访问地址:http://localhost:8080/index/get
看到控制台打印信息:Received <Hello from RabbitMQ!>
总结:RabbitMQ和我们数据库连接 hibernate spring 都有工厂 都有类似连接这样的概念。
Connection 这种概念就是我们配置上面都有类似性,可以通过查询源码来看到这些信息。
比如看下这个com.rabbitmq.client.ConnectionFactory的源码
/** @deprecated */ @Deprecated public static final int DEFAULT_NUM_CONSUMER_THREADS = 5; public static final String DEFAULT_USER = "guest"; public static final String DEFAULT_PASS = "guest"; public static final String DEFAULT_VHOST = "/"; public static final int DEFAULT_CHANNEL_MAX = 0; public static final int DEFAULT_FRAME_MAX = 0; public static final int DEFAULT_HEARTBEAT = 0; public static final String DEFAULT_HOST = "localhost"; public static final int USE_DEFAULT_PORT = -1; public static final int DEFAULT_AMQP_PORT = 5672; public static final int DEFAULT_AMQP_OVER_SSL_PORT = 5671; public static final int DEFAULT_CONNECTION_TIMEOUT = 0; public static final int DEFAULT_SHUTDOWN_TIMEOUT = 10000; private static final String DEFAULT_SSL_PROTOCOL = "TLSv1"; private String username = "guest"; private String password = "guest"; private String virtualHost = "/"; private String host = "localhost"; private int port = -1; private int requestedChannelMax = 0; private int requestedFrameMax = 0; private int requestedHeartbeat = 0; private int connectionTimeout = 0; private int shutdownTimeout = 10000; private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); private SocketFactory factory = SocketFactory.getDefault(); private SaslConfig saslConfig; private ExecutorService sharedExecutor; private ThreadFactory threadFactory; private SocketConfigurator socketConf; private ExceptionHandler exceptionHandler; private boolean automaticRecovery; private boolean topologyRecovery; private long networkRecoveryInterval;
从上面的源码我们可以看出 凡是都是可配置的。这些代码的配置替代xml也是可以的。xml有时候明朗化都可以。不同的方式配置不一样而已。
相关推荐
本项目“spring boot rabbitmq学习练习demo源码”结合了这两者,提供了一个学习和实践Spring Boot集成RabbitMQ的实例。 首先,让我们深入了解Spring Boot与RabbitMQ的集成。在Spring Boot项目中,我们可以通过添加`...
### Spring Boot RabbitMQ 实现延迟消息详解 #### 引言 在现代分布式系统设计中,消息中间件(如RabbitMQ)被广泛用于处理异步通信、解耦服务以及优化性能等方面。其中,延迟消息是一种重要的功能,它可以实现在...
基于 Spring Boot rabbitmq redis mysql 的电商秒杀系统.zip
RabbitMQ 是一个开源的消息代理中间件,广泛用于构建分布式应用程序中的消息系统。在 Spring Boot 项目中,通过集成 RabbitMQ,可以实现异步消息传递、消息队列等功能,提高系统的可靠性和扩展性。
在Spring Boot项目中,可以在application.yml或application.properties文件中设置: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / ``` 然后,...
在Spring Boot框架中集成RabbitMQ,可以轻松实现消息队列的功能,提高系统的异步处理能力和可扩展性。本文将详细介绍如何在Spring Boot项目中配置并使用RabbitMQ。 首先,我们需要了解RabbitMQ的基本概念。RabbitMQ...
Spring Boot 配置多个 RabbitMQ Spring Boot 是一个流行的 Java 框架,用于快速构建生产级别的应用程序。RabbitMQ 是一个消息队列中间件,用于实现异步消息处理和队列管理。配置多个 RabbitMQ 实例是为了提高系统的...
RabbitMQ连接池+SpringBoot实现。通过连接池实现将高效的管理RabbitMQ的Connection,并与springboot进行整合,实现消息发送,获取队列列表等功能。基于此可以进行更多功能的扩充。
Spring Boot集成rabbitMQ实现消息推送,rabbitMQ为异步消息处理提出了一个很好的解决方案,它是一个非常好用的消息中间件。主要解决当生产者大量产生数据时,消费者无法快速消费的问题。这个时候需要一个中间层,...
首先,我们要理解Spring对RabbitMQ的支持主要体现在Spring AMQP项目中,它为RabbitMQ提供了一套高级抽象层,使得开发者能够更加便捷地使用RabbitMQ。在整合Spring和RabbitMQ时,我们需要引入相应的依赖。在Maven工程...
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持。下面通过本文给大家介绍下spring boot整合RabbitMQ(Direct模式),需要的朋友可以...
基于Spring Boot和RabbitMQ的消息处理系统 项目简介 本项目是一个基于Spring Boot框架和RabbitMQ消息队列的消息处理系统。它涵盖了消息的生产、消费、路由、交换机配置以及高级特性如发布确认和RPC调用。通过...
Springboot整合RabbitMQ最简单demo,适用于springcloud项目,作为消息总线适用,需要安装RabbitMQ,Mac linux可以使用命令行一键安装,在项目配置文件配置好端口即可(已默认配置),启动项目访问8080端口,参数见controller.
在本文中,我们将深入探讨如何使用Spring Boot与RabbitMQ结合来发送邮件,并确保消息100%投递成功并被消费。RabbitMQ是一个强大的消息队列系统,它允许我们在分布式系统中可靠地传递消息。 首先,我们需要理解...
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-boot-...