ActiveMq和RabbitMq类似:
ActiveMq:https://my.oschina.net/xiaoxishan/blog/381209#OSC_h3_1
以下介绍RabbitMq:
http://www.cnblogs.com/LipeiNet/p/5980802.html
win10安装:
https://blog.csdn.net/nnsword/article/details/79544349
rabbitmq 中各个方法不同传入参数不同如交换机和路由队列等不同,所表示的意义不同。
首先下载安装后启动RabbitMq服务器:参考tomcat服务器,访问地址:http://localhost:15672 用户/密码 guest/guest
配置文件配置项:加载mq模板信息,配置mq代理类,持久化消息队列,交换器队列绑定(重要),生产者,消费者,监听.
生产者发送消息: rabbitTemplate.convertAndSend(object) 参数不能为空
消费者接收消息: rabbitTemplate.receiveAndConvert() 参数可以为空
Mq基础知识:
direct exchange 发送消息是要看routingKey的 (默认方法)
fanout :广播:
Topic :订阅:
Headers : 使用的比较少
channel.BasicPublish(exchange, “TaskQueue”, properties, bytes);
channel.BasicPublish(“”, “TaskQueue”, properties, bytes);
topic exchange 有时候的行为会像其他类型的exchange,比如说:
当routingKey只是有#号的时候,它的行为和fanout的行为是一样的。
当routingKey什么的没有,空字符串的时候,它的行为是和direct是一样的。
生产者:
@Component
public class Producer {
@Resource
private RabbitTemplate rabbitTemplate;
public void sendMessage(RabbitMessage msg) {
try {
System.out.println(rabbitTemplate.getConnectionFactory().getHost());
System.out.println(rabbitTemplate.getConnectionFactory().getPort());
// 发送信息
rabbitTemplate.convertAndSend(msg.getExchange(), msg.getRouteKey(), msg.getParams().toString());
} catch (Exception e) {
}
}
}
消费者:
public class Consumer {
@Resource
private RabbitTemplate rabbitTemplate;
public void rmqProducerMessage(Object object) {
RabbitMessage rabbitMessage = (RabbitMessage) object;
System.out.println(rabbitMessage.getExchange());
System.out.println(rabbitMessage.getRouteKey());
System.out.println(rabbitMessage.getParams().toString());
System.out.println(rabbitTemplate.receiveAndConvert(rabbitMessage.getRouteKey()));
}
}
发布与接收消息:
@Controller
@RequestMapping("/rabbit")
public class rabbitController {
@Resource
private Producer pro;
@Resource
private Consumer con;
@Resource
private RabbitTemplate rabbitTemplate;
@RequestMapping("/rabbit")
public String index() {
String exchange = "testExchange"; 交换器
String routeKey = "testQueue";// 队列
String message = "helloword-wee";// 调用的方法
// 参数
Map<String, Object> param = new HashMap<String, Object>();
param.put("data", "hello");
param.put("data1", "hello");
param.put("data2", "hello");
RabbitMessage msg = new RabbitMessage(exchange, routeKey, message);
// 发送消息
pro.sendMessage(msg);
con.rmqProducerMessage(msg);
return "";
}
消息类:
public class RabbitMessage {
private static final long serialVersionUID = -6487839157908352120L;
private Class<?>[] paramTypes;// 参数类型
private String exchange;// 交换器
private Object[] params;
private String routeKey;// 路由key
public RabbitMessage() {
}
public RabbitMessage(String exchange, String routeKey, Object... params) {
this.params = params;
this.exchange = exchange;
this.routeKey = routeKey;
}
@SuppressWarnings("rawtypes")
public RabbitMessage(String exchange, String routeKey, String methodName, Object... params) {
this.params = params;
this.exchange = exchange;
this.routeKey = routeKey;
int len = params.length;
Class[] clazzArray = new Class[len];
for (int i = 0; i < len; i++)
clazzArray[i] = params[i].getClass();
this.paramTypes = clazzArray;
}
public byte[] getSerialBytes() {
byte[] res = new byte[0];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.close();
res = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
public String getRouteKey() {
return routeKey;
}
public String getExchange() {
return exchange;
}
public void setExchange(String exchange) {
this.exchange = exchange;
}
public void setRouteKey(String routeKey) {
this.routeKey = routeKey;
}
public Class<?>[] getParamTypes() {
return paramTypes;
}
public Object[] getParams() {
return params;
}
}