Rabbitmq

使用RabbitMQ Java Client

2012-06-03
Rabbitmq

Publishing Messages #

  1. 连接到Broker
  2. 获取Channel
  3. 声明一个Exchange
  4. 创建一个Message
  5. 发布这个Message
  6. 关闭Channel
  7. 关闭Connection
 1ConnectionFactory connectionFactory = new ConnectionFactory();
 2// AMQP URI amqp://userName:password@hostName:portNumber/virtualHost"
 3connectionFactory.setUri("amqp://test:[email protected]:5672/demo");
 4Connection connection = connectionFactory.newConnection();
 5Channel channel = connection.createChannel();
 6channel.exchangeDeclare("first-exchange", "direct", true);
 7byte[] msgBody = "Hello, World!".getBytes("UTF-8");
 8channel.basicPublish("first-exchange", "helloRoutingKey", null, msgBody);
 9channel.close();
10connection.close();

Receiving Messages by Subscription #

 1public static void main(String[] args) throws Exception {
 2    ConnectionFactory connectionFactory = new ConnectionFactory();
 3    connectionFactory.setUri("amqp://test:[email protected]:5672/demo");
 4    Connection connection = connectionFactory.newConnection();
 5    final Channel channel = connection.createChannel();
 6    channel.exchangeDeclare("first-exchange", "direct", true);
 7    channel.queueDeclare("hello-queue", true, false, false, null);
 8    channel.queueBind("hello-queue", "first-exchange", "helloRoutingKey");
 9    channel.basicConsume("hello-queue", new DefaultConsumer(channel) {
10        @Override
11        public void handleDelivery(String consumerTag, Envelope envelope, 
12        	BasicProperties properties, byte[] body)
13            throws IOException {
14            String msg = new String(body, "UTF-8");
15            System.out.println(msg);
16            channel.basicAck(envelope.getDeliveryTag(), false);
17        }
18    });
19    Scanner scanner = new Scanner(System.in);
20    scanner.nextLine();
21    scanner.close();
22    channel.close();
23    connection.close();
24}

安装RabbitMQ

2012-06-02
Rabbitmq

RabbitMQ是一个开源的AMQP实现,服务端使用Erlang语言编写,支持多种客户端, 如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOPMP等。

构建和安装Erlang #

提前安装所需依赖:

...

AMQP协议简介

2012-06-01
Rabbitmq

简介 #

AMQP即Advanced Message Queuing Protocol,高级消息队列协议,是面向消息中间件设计的应用层协议的一个开放标准。 它的主要特点是面向消息、队列、路由(包括点对点和发布/订阅)]、可靠性和安全。

AMQP允许来自不同供应商的消息生产者和消费者实现真正的互操作扩展,就如同SMTP、HTTP、FTP等协议采用的方式一样。而此前对于消息中间件的标准化努力则集中在API层面上(比如JMS),且没有提供互操作性的途径。不同于JMS的仅仅定义API,AMQP是一个线路级的协议——它描述了通过网络传输的字节流的数据格式。因此,遵从这个协议的任何语言编写的工具均可以操作AMQP消息。

...

© 2024 青蛙小白