rabbitmq 学习-4-初试2

RpcClient,RpcServer同步发送接收消息
Channel.basicPublish,Channel.basicGet异步发送接收消息
本例是一个简单的步发送消息实例
1,发送端
public class Publish {

    private static Connection connection;

    static {
        ConnectionParameters params = new ConnectionParameters();
        ConnectionFactory factory = new ConnectionFactory(params);
        try {
            connection = factory.newConnection("localhost", AMQP.PROTOCOL.PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Channel channel = connection.createChannel();

            RpcClient rpc = new RpcClient(channel, "exchangeName", "routingKey");
            byte[] primitiveCall = rpc.primitiveCall("hello world".getBytes());
            System.out.println(new String(primitiveCall));
            primitiveCall = rpc.primitiveCall("hello world2".getBytes());
            System.out.println(new String(primitiveCall));

            rpc = new RpcClient(channel, "exchangeName", "routingKey2");
            primitiveCall = rpc.primitiveCall("hello world2".getBytes());
            System.out.println(new String(primitiveCall));

            System.out.println("publish success.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2,接收端
public class Receive {

    private static Connection connection;

    static {
        ConnectionParameters params = new ConnectionParameters();
        ConnectionFactory factory = new ConnectionFactory(params);
        try {
            connection = factory.newConnection("localhost", AMQP.PROTOCOL.PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Channel channel = connection.createChannel();
            System.out.println(channel.toString());

            channel.exchangeDeclare("exchangeName", "topic");
            channel.exchangeDeclare("exchangeName2", "topic");
            channel.queueDeclare("queueName");
            channel.queueBind("queueName", "exchangeName", "routingKey");
            channel.queueBind("queueName", "exchangeName", "routingKey2");
            channel.queueBind("queueName", "exchangeName2", "routingKey2");
            channel.queueBind("queueName", "exchangeName2", "routingKey");
//queue 与 exchange 是多对多的,可以把同一queue和exchange以多个不同的routing进行bind,这样就会有多个routing,而不是一个,虽然说这些rout 是绑定相同的 exchange, queue

            final RpcServer rpcServer = new RpcServer(channel, "queueName") {
                @Override
                public byte[] handleCall(byte[] requestBody, AMQP.BasicProperties replyProperties) {
                    System.out.println("receive msg: " + new String(requestBody));
                    return "return message".getBytes();
                }
            };

            Runnable main = new Runnable() {
                @Override
                public void run() {
                    try {
                        throw rpcServer.mainloop();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
            new Thread(main).start();

            System.out.println("receive success.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值