1.什么是rabbitmq
RabbitMQ是一个在AMQP基础上完成的,可复用的企业消息系统
2.rabbitmq的安装
先安装erlang语言包 在安装rabbitmq
3.使用spring整合rabbitmq
3.1 服务端的配置
3.1.1 rabbitmq.properties文件
#rabbitmq的基本配置
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=zy
rabbitmq.password=zy123456
rabbitmq.vhost=/zy
3.1.2 依赖包的引入
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
3.1.3 整合配置
//配置文件类
package com.zy.rabbitmq;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("com.zy.rabbitmq")
@PropertySource("classpath:/rabbitmq.properties")
public class AppConfig {
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq.port}")
private int port;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.vhost}")
private String vhost;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
}
//配置mq连接工厂
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public ConnectionFactory connectionFactory (){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(vhost);
return connectionFactory;
}
//管理
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public RabbitAdmin rabbitAdmin(){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
//rabbitAdmin.declareQueue(queue());
//rabbitAdmin.declareExchange(topicExchange());
//rabbitAdmin.declareBinding(BindingBuilder.bind(queue()).to(topicExchange()).with(""));
return rabbitAdmin;
}
/*@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public Queue queue (){
Queue queue = new Queue("myQueue");
return queue;
}*/
//交换机的设置
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public TopicExchange topicExchange (){
TopicExchange exchange = new TopicExchange("myExchange");
return exchange;
}
//Java操作mq的模板
@Bean
public RabbitTemplate rabbitTemplate(){
RabbitTemplate template = new RabbitTemplate(connectionFactory ());
return template;
}
}
3.1.4 消息发送类
import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Send {
@Autowired
private RabbitTemplate template;
public void sendMsg() throws InterruptedException{
Map<String,Object> map =new HashMap<String, Object>();
map.put("username", "张三");
map.put("password", "123456");
map.put("type", System.currentTimeMillis());
JSONObject object = JSONObject .fromObject(map);
String json = object.toString();
template.convertAndSend("myExchange","update",json); //消息的发送
System.out.println("消息已发出");
}
}
3.2 服务层的配置
3.2.1 依赖的引入
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
3.2.2 配置参数文件
rabbitmq.properties
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=zy
rabbitmq.password=zy123456
rabbitmq.vhost=/zy
3.2.3 整合配置
package com.zy.forward;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("com.zy.forward")
@PropertySource("classpath:/rabbitmq.properties")
public class AppConfig {
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq.port}")
private int port;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.vhost}")
private String vhost;
//配置mq连接工厂
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public ConnectionFactory connectionFactory (){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(vhost);
return connectionFactory;
}
//管理
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public RabbitAdmin rabbitAdmin(){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
//rabbitAdmin.declareQueue(queue());
//rabbitAdmin.declareExchange(topicExchange());
//rabbitAdmin.declareBinding(BindingBuilder.bind(queue()).to(topicExchange()).with(""));
return rabbitAdmin;
}
/*@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public Queue queue (){
Queue queue = new Queue("myQueue");
return queue;
}*/
/* @Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public TopicExchange topicExchange (){
TopicExchange exchange = new TopicExchange("myExchange");
return exchange;
}
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public SimpleMessageListenerContainer container (){
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory ());
container.setQueueNames("myQueue");
container.setMessageListener(new MessageListenerAdapter(demoMessageListener()));
container.setAcknowledgeMode(AcknowledgeMode.AUTO);
return container;
}
@Bean
public DemoMessageListener demoMessageListener(){
return new DemoMessageListener();
}
}
3.2.4 接收消息类
package com.zy.forward;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import com.rabbitmq.client.Channel;
public class DemoMessageListener implements ChannelAwareMessageListener{
@Override
public void onMessage(Message message, Channel channel) throws Exception {
String messageBody = new String(message.getBody(), "UTF-8");
System.out.println(messageBody);
}
}