rabbitmq 基本的构建消息应用的方式/rabbitmq工作模式

Github:https://github.com/liubin192837/rabbitmPractice

 "Hello World!"

推送端:

src/main/java/com/lb/demo/utils.java
public static void p2pSend() throws IOException, TimeoutException {
    String queueName = "p2pTest";
    Channel channel1 = getChannel();
    channel1.queueDeclare(queueName, false, false, false, null);
    for (int i = 0; i < NUMBER; i++) {
        String message = "Sent from p2pSend:message " + i;
        channel1.basicPublish("", queueName, null, message.getBytes());
        printPrompt("p2pSend", "message " + i + "'");
    }
    closeChannelAndConnection(channel1);
}

消费端:

customer/src/main/java/com/lb/customer/Utils.java:
//Hello world/P2P mode 
public static void p2pConsumer(String flagName, String exchangeName, String key, String mode, String queueName) 
        throws TimeoutException, IOException {
    Channel channel = null;
    channel = getChannel();
    channel.queueDeclare(queueName, false, false, false, null);
    printPrompt(flagName, true, false, null);
    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope,
                                   AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            String message = new String(body, "UTF-8");
            printPrompt(flagName, false, true, message);
        }
    };
    channel.basicConsume(queueName, true, consumer);

}

Work queues

推送端:

src/main/java/com/lb/demo/utils.java
public static void workQueuesSent() throws IOException, TimeoutException {
    String queueName = "workQueues";
    Channel channel = getChannel();
    channel.queueDeclare(queueName, false, false, false, null);
    for (int i = 0; i < NUMBER; i++) {
        String message = "Sent from workQueuesSent: message " + i;
        channel.basicPublish("", queueName, null, message.getBytes());
        printPrompt("workQueuesSent", "message " + i + "'");
    }
    closeChannelAndConnection(channel);
}

消费端(两部分):

1)基本的消费单元
customer/src/main/java/com/lb/customer/Utils.java:
//Work Queues
public static void workQueuesConsumer(String flagName, String exchangeName, String key, String mode, String queueName)
        throws TimeoutException, IOException {
    Channel channel = null;
    channel = getChannel();
    channel.queueDeclare(queueName, false, false, false, null);
    printPrompt(flagName, true, false, null);
    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope,
                                   AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            String message = new String(body, "UTF-8");
            printPrompt(flagName, false, true, message);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}
2)这里产生两个来消费端
//WorkQueues mode
private static void consumerWorkQueues() {
   String queueName = "workQueues";
   try {
      Utils.workQueuesConsumer("consumerWorkQueuesOne", null, null, null, queueName);
      Utils.workQueuesConsumer("consumerWorkQueuesTwo", null, null, null, queueName);
   } catch (IOException e) {
      e.printStackTrace();
   }
   catch (TimeoutException e) {
      e.printStackTrace();
   }
}

Publish/Subscribe

推送端:

src/main/java/com/lb/demo/utils.java
public static void publishSubscribeSent() throws IOException, TimeoutException {
    Channel channel = getChannel();
    channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANE_NAME_PUBLISH_SUBSCRIBLE, FANOUT);
    for (int i = 0; i < NUMBER; i++) {
        String message = "Sent from PublishSubscribeSent: message " + i;
        channel.basicPublish(EXCHANE_NAME_PUBLISH_SUBSCRIBLE, "", null, message.getBytes());
        printPrompt("PublishSubscribeSent", "message " + i + "'");
    }
    closeChannelAndConnection(channel);
}

消费端(两部分):

1) customer/src/main/java/com/lb/customer/Utils.java
public static void publishSubscribeConsumer(String flagName, String exchangeName, String key, String mode, String queueName)
        throws TimeoutException, IOException{
    Channel channel = null;
    channel = getChannel();
    channel.exchangeDeclare(exchangeName, mode);
    String queueTempName = channel.queueDeclare().getQueue();
    channel.queueBind(queueTempName, exchangeName,"");
    printPrompt(flagName, true, false, null);
    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            String message = new String(body, "UTF-8");
            printPrompt(flagName, false, true, message);
        }
    };
    channel.basicConsume(queueTempName, true, consumer);
}
2)
//PublishSubscribe
private static void consumerPublishSubscribe() {
   String EXCHANE_NAME = "exchange_name_publish_subscribe";
   String MODE = "fanout";
   try {
      //Here create two consumers
      Utils.publishSubscribeConsumer("consumerPublishSubscribe_One", EXCHANE_NAME, null, MODE, null);
      Utils.publishSubscribeConsumer("consumerPublishSubscribe_two", EXCHANE_NAME, null, MODE, null);
   } catch (IOException e) {
      e.printStackTrace();
   } catch (TimeoutException e) {
      e.printStackTrace();
   }
}

Routing

推送端:

src/main/java/com/lb/demo/utils.java
 public static void routingSend() throws IOException, TimeoutException {
        Channel channel = getChannel();
        channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANE_NAME_ROUTE, DIRECT);
        for (int i = 0; i < NUMBER; i++) {
            String message2 = "Sent from routingSend: message " + i;
            channel.basicPublish(EXCHANE_NAME_ROUTE, ROUTE_KEY, null, message2.getBytes());
            printPrompt("routingSend", "message " + i + "'");
        }
        closeChannelAndConnection(channel);
    }

消费端(两部分):

1)customer/src/main/java/com/lb/customer/Utils.java
//ROUTE mode
public static void routeConsumer(String flagName, String exchangeName, String key, String mode, String queueName)
        throws TimeoutException, IOException {
    topicConsumer(flagName, exchangeName, key, mode, queueName);
}
2)
//Route mode
private static void consumerRoute() {
   String KEY1 = "key.route";
   String KEY2 = "key.route.#";
   String EXCHANE_NAME = "exchange_name_route";
   String MODE = "direct";
   try {
      //Here create two consumers
      Utils.routeConsumer("consumerRouteOne",EXCHANE_NAME, KEY1, MODE, null);
      Utils.routeConsumer("consumerRouteTwo", EXCHANE_NAME, KEY2, MODE, null);
   } catch (IOException e) {
      e.printStackTrace();
   } catch (TimeoutException e) {
      e.printStackTrace();
   }
}

Topics

推送端:

src/main/java/com/lb/demo/utils.java
    public static void topicSend() throws IOException, TimeoutException {
        Channel channel = getChannel();
        channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANE_NAME_TOPIC, TOPIC);
        for (int i = 0; i < NUMBER; i++) {
            String message2 = "Sent from topicSend: message " + i;
            channel.basicPublish(EXCHANE_NAME_TOPIC, TOPIC_KEY, null, message2.getBytes());
            printPrompt("topicSend", "message " + i + "'");
        }
        closeChannelAndConnection(channel);
    }

消费端(两部分):

1)customer/src/main/java/com/lb/customer/Utils.java
//Topic mode
public static void topicConsumer(String flagName, String exchangeName, String key, String mode, String queueName)
        throws TimeoutException, IOException {
    Channel channel = null;
    channel = getChannel();
    channel.exchangeDeclare(exchangeName, mode);
    String queueTempName = channel.queueDeclare().getQueue();
    channel.queueBind(queueTempName, exchangeName, key);
    printPrompt(flagName, true, false, null);
    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            String message = new String(body, "UTF-8");
            printPrompt(flagName, false, true, message);
        }
    };
    channel.basicConsume(queueTempName, true, consumer);
}
2)
//Topic mode
private static void consumerTopic() {
       String KEY = "key.#";
   String EXCHANE_NAME = "exchange_name";
   String MODE = "topic";
   try {
      //Here create two consumers
      Utils.topicConsumer("consumerTopicOne",EXCHANE_NAME, KEY, MODE, null);
      Utils.topicConsumer("consumerTopicTwo", EXCHANE_NAME, KEY, MODE, null);
   } catch (IOException e) {
      e.printStackTrace();
   } catch (TimeoutException e) {
      e.printStackTrace();
   }
}

参考:
http://www.rabbitmq.com/getstarted.html

猜你喜欢

转载自blog.csdn.net/liubin192837/article/details/81101794