工作原理:
交换机类型
heard和fanout不需要设置routingkey
topic和dirct需要设置routingkey
详细接受类型https://www.cnblogs.com/linyufeng/p/9885020.html
生成者 ->连接scoket->mq(交互机)->
生成者
1.连接conntionfactory设置虚拟机
2.创建conntion
3.创建channel
4.声明channel
channel.queueDeclare(QUEUE,true,false,false,null);
5.自定交换机
channel.basicPublish("",QUEUE,null,message.getBytes());
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
//生产者入门程序
public class rabbitmq {
private static final String QUEUE = "helloworld";
public static void main(String args[]){
//通过连接工程连接mq
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//设置虚拟机
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
//建立连接
connection = connectionFactory.newConnection();
//创建管道
channel = connection.createChannel();
//声明队列
/**
* Like {@link Channel#queueDeclare(String, boolean, boolean, boolean, java.util.Map)} but sets nowait
* flag to true and returns no result (as there will be no response from the server).
* @param queue the name of the queue
* @param durable true if we are declaring a durable queue (the queue will survive a server restart)
* @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
* @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
* @param arguments other properties (construction arguments) for the queue扩展参数连接时间
* @throws java.io.IOException if an error is encountered
*/
channel.queueDeclare(QUEUE,true,false,false,null);
//指定交互机
/**
* Publish a message.
*
* Publishing to a non-existent exchange will result in a channel-level
* protocol exception, which closes the channel.
*
* Invocations of <code>Channel#basicPublish</code> will eventually block if a
* <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
*
* @see com.rabbitmq.client.AMQP.Basic.Publish
* @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
* @param exchange the exchange to publish the message to
* @param routingKey the routing key如果没有使用交换机也就是使用默认交互机使用队列名称QUEUE
* @param mandatory true if the 'mandatory' flag is to be set
* @param immediate true if the 'immediate' flag is to be
* set. Note that the RabbitMQ server does not support this flag.
* @param props other properties for the message - routing headers etc
* @param body the message body
* @throws java.io.IOException if an error is encountered
*/
String message = "first mq";
channel.basicPublish("",QUEUE,null,message.getBytes());
System.out.println("发送成功");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
消费者
1.创建ConnectionFactory
2.创建连接
3.创建channel
4.声明队列
5.消费消息方式
6.监听队列
import ch.qos.logback.core.encoder.ByteArrayUtil;
import com.rabbitmq.client.*;
import com.sun.deploy.util.StringUtils;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;
public class rabbitMqCuserm {
private static final String QUEUE = "helloworld";
public static void main (String args[]){
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setVirtualHost("/");
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
Connection connection = null;
try {
//创建连接
connection = connectionFactory.newConnection();
//场景管道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE,true,false,false,null);
//消费方法
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
//当接受到消息时此方法别调用
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
/**
* String consumerTag,用来标识消费者在监听队列时设置
* Envelope envelope, 信封 获取交换机
* AMQP.BasicProperties properties, 消息属性
* byte[] body 消息内容
*/
//获取交换机
String exchange = envelope.getExchange();
//消息id,mq在chanal中的标识,确认消息接受
long dekuverTag = envelope.getDeliveryTag();
String message = new String(body);
System.out.println(message);
}
};
//监听队列
/**
* Start a non-nolocal, non-exclusive consumer, with
* a server-generated consumerTag.
* @param queue the name of the queue
* @param autoAck true if the server should consider messages
* acknowledged once delivered; false if the server should expect
* explicit acknowledgements
* @param callback an interface to the consumer object
* @return the consumerTag generated by the server
* @throws java.io.IOException if an error is encountered
* @see com.rabbitmq.client.AMQP.Basic.Consume
* @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
* @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)
*/
channel.basicConsume(QUEUE,true,defaultConsumer);
System.out.println("接受成功");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
队列模式 work queues:一个生成者一个队列多个消费者,一对多一个生成者将消息发送给交换机
消息不能被重复消费,论循算法。实际运用
发布订阅模式:(publish/subscribe)
type:FANOUT
一个生成者多个队列
一个消息被多个消费者同时接受,
应用:银行转账
routing:路由模式
一个生产者一个交换机多个消费者
一个队列获取多个routingkey
队列和交换机绑定时指定路由key
topic模式
#匹配一个或多个词
*匹配一个词
每个词以.分割
队列在绑定交换机是配置routkey通配符
header模式取消routkey但需要设置keyvalue对
rpc客户端调用服务端