RabbitMQ生产消费模式编程

生产者(hello_producer.py)

import pika, sys

credentials = pika.PlainCredentials("rabbit", "123456")
conn_params = pika.ConnectionParameters("localhost", credentials = credentials)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
channel.exchange_declare("hello-exchange","direct",False,False,False)

msg = sys.argv[1]
msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"
channel.basic_publish(body=msg,exchange="hello-exchange",properties=msg_props,routing_key="hola")

消费者(TestConsumer.java)

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class TestConsumer {

    public static void main(String[] args) {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.100.177");
        factory.setPort(5672);
        factory.setUsername("rabbit");
        factory.setPassword("123456");
        factory.setVirtualHost("/");

        Connection connection = null;
        try {
            connection = factory.newConnection();
            Channel channel = connection.createChannel();

            String exchangeName = "hello-exchange";
            String routingKey = "hola";
            String queueName = "queueOne";
            channel.exchangeDeclare(exchangeName,"direct");
            channel.queueDeclare(queueName,false,false,false,null);
            channel.queueBind(queueName,exchangeName,routingKey);

            Consumer consumer = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //消费消费
                    String msg = new String(body,"utf-8");
                    System.out.println("consume msg: "+msg);
                    try {
                        TimeUnit.MILLISECONDS.sleep((long) (Math.random()*500));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //手动消息确认
                    getChannel().basicAck(envelope.getDeliveryTag(),false);
                }
            };

            //调用消费消息
            channel.basicConsume(queueName,false,"queueOne", consumer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

}

pom.xml中引入rabbitmq依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <groupId>com.jx</groupId>
    <artifactId>rabbitmqtest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.6.6</version>
        </dependency>
    </dependencies>

</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值