爱站权重,wordpress自动发货如何设置,美容茌哪个网站做宣传好,广告创意设计的影响因素Hello World
Hello World是官网给出的第一个模型#xff0c;使用的交换机类型是直连direct#xff0c;也是默认的交换机类型。 在上图的模型中#xff0c;有以下概念#xff1a;
P#xff1a;生产者#xff0c;也就是要发送消息的程序C#xff1a;消费者#xff1a;消…Hello World
Hello World是官网给出的第一个模型使用的交换机类型是直连direct也是默认的交换机类型。 在上图的模型中有以下概念
P生产者也就是要发送消息的程序C消费者消息的接受者会一直等待消息到来。Queue消息队列图中红色部分。类似一个邮箱可以缓存消息生产者向其中投递消息消费者从其中取出消息。
此模型中只有一个生产者、一个队列、一个消费者。
这种模式适合于消息任务不是很密集并且处理任务不算太过耗时的场景。消费者消费的速度赶得上生产者生产的速度。
创建生产者
public class MyProducer {Testpublic void test() throws Exception {// 队列名称String queue xw_queue;String message Hello World - ;// 创建工厂ConnectionFactory factory new ConnectionFactory();factory.setVirtualHost(/);factory.setHost(xuewei.world);factory.setUsername(xuewei);factory.setPassword(123456);factory.setPort(5672);// 创建连接和通道Connection connection factory.newConnection();Channel channel connection.createChannel();for (int i 0; i 10; i) {// 发布消息channel.basicPublish(xw_exchange, queue, null, (message i).getBytes());}}
}创建消费者
public class MyConsumer {public static void main(String[] args) throws Exception {// 队列名称String queue xw_queue;// 创建工厂ConnectionFactory factory new ConnectionFactory();factory.setVirtualHost(/);factory.setHost(xuewei.world);factory.setUsername(xuewei);factory.setPassword(123456);factory.setPort(5672);// 创建连接和通道Connection connection factory.newConnection();Channel channel connection.createChannel();channel.queueDeclare(queue, true, false, false, null);channel.queueBind(, xw_exchange, queue);channel.basicConsume(queue, true, new DefaultConsumer(channel) {Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println(接收到消息: new String(body));// TODO 业务处理}});}
}