zoukankan      html  css  js  c++  java
  • Hello World模式

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

    https://github.com/rabbitmq/rabbitmq-tutorials

    环境搭建

    这里使用gradle项目

    apply plugin: 'eclipse'
    apply plugin: 'maven'
    apply plugin: 'java'
        
    sourceCompatibility = 1.8
    ​
    [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8' 
    ​
    repositories {
       
        maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        mavenCentral()
        jcenter()
    }
    dependencies {
    ​
        compile group: 'com.rabbitmq', name: 'amqp-client', version: '4.1.0'
        compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    }

    Hello World模式(点对点)

     1 package com.rabbitmq.www.helloworld;
     2 
     3 
     4 
     5 import com.rabbitmq.client.Channel;
     6 import com.rabbitmq.client.Connection;
     7 import com.rabbitmq.client.ConnectionFactory;
     8 
     9 public class Send {
    10     
    11     
    12     private final static String QUEUE_NAME = "hello";
    13     
    14     
    15     private final static String HOST_ADDR = "172.18.112.102";
    16     
    17     
    18     
    19     public static void main(String[] args) throws Exception {
    20         
    21         ConnectionFactory factory = new ConnectionFactory();
    22         factory.setHost(HOST_ADDR);
    23         Connection connection = factory.newConnection();
    24         Channel channel = connection.createChannel();
    25         channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    26         String message = "Hello World!";
    27         channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    28         System.out.println("发送消息 : " + message + "'");
    29         channel.close();
    30         connection.close();
    31         
    32     }
    33         
    34     
    35 
    36 }
    package com.rabbitmq.www.helloworld;
    
    import java.io.IOException;
    
    import com.rabbitmq.client.AMQP;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Consumer;
    import com.rabbitmq.client.DefaultConsumer;
    import com.rabbitmq.client.Envelope;
    
    public class Recv {
    
        private final static String QUEUE_NAME = "hello";
        
        private final static String HOST_ADDR = "172.18.112.102";
        
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
    
            
            
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(HOST_ADDR);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            System.out.println("等待消息");
            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");
                  System.out.println("接受消息 : " + message);
                }
              };
              channel.basicConsume(QUEUE_NAME, true, consumer);
    
        }
    
    }
  • 相关阅读:
    使用Eolinker拓展API设计
    如何记录API
    API设计
    【翻译】通过API主导的集成来证明您的业务未来
    从状态库迁移分布系统建设方式
    PostgreSql 查询某个模式下面的所有表
    迁移状态库的地市区县信息
    测开和开发的难度对比
    yum源的三种搭建方式
    Harbor实现容器镜像仓库的管理和运维
  • 原文地址:https://www.cnblogs.com/woms/p/7040842.html
Copyright © 2011-2022 走看看