`

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();
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics