Hello World模式

Posted woms

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

    }

}

 

以上是关于Hello World模式的主要内容,如果未能解决你的问题,请参考以下文章

Python Hello World

Hello World模式

MVC 模式中的“Hello World”

Graphql Java hello world:向父模式添加子模式失败

三RabbitMQ中模式—简单模式Hello World

c语言入门到入门,hello world篇