zoukankan      html  css  js  c++  java
  • RabbitMQ Hello World

    RabbitMQ Hello World

    rabbitmq operation:

    C:Program FilesRabbitMQ Server
    abbitmq_server-3.7.2sbin>rabbitmqctl.bat add_user root root123
    Adding user "root" ...
    C:Program FilesRabbitMQ Server
    abbitmq_server-3.7.2sbin>rabbitmqctl.bat set_permissions -p / root ".*" ".*" ".*"
    Setting permissions for user "root" in vhost "/" ...
    
    C:Program FilesRabbitMQ Server
    abbitmq_server-3.7.2sbin>rabbitmqctl.bat start_app
    Starting node rabbit@LAPTOP-07APR6RI ...
     completed with 3 plugins.
    C:Program FilesRabbitMQ Server
    abbitmq_server-3.7.2sbin>rabbitmqctl.bat list_users
    Listing users ...
    guest   [administrator]
    root    []
    
    C:Program FilesRabbitMQ Server
    abbitmq_server-3.7.2sbin>rabbitmqctl.bat set_user_tags root administrator
    Setting tags for user "root" to [administrator] ...

    pom:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.stono</groupId>
      <artifactId>rmq_p010</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <name>rmq_p010</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.rabbitmq</groupId>
          <artifactId>amqp-client</artifactId>
          <version>4.2.1</version>
        </dependency>
    
      </dependencies>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.7.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.20.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

    producer:

    package com.stono;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.MessageProperties;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    public class RabbitProducer {
        private static final String EXCHANGE_NAME = "exchange_demo";
        private static final String ROUTING_KEY = "routingkey_demo";
        private static final String QUEUE_NAME = "queue_demo";
        private static final String IP_ADDRESS = "192.168.1.103";
        private static final int PORT = 5672;
    
        public static void main(String[] args) throws IOException, TimeoutException {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(IP_ADDRESS);
            factory.setPort(PORT);
            factory.setUsername("root");
            factory.setPassword("root123");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGE_NAME, "direct",true, false, null);
            channel.queueDeclare(QUEUE_NAME,true,false,false,null);
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
            String message = "Hello World";
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            channel.close();
            connection.close();
        }
    }

    consumer:

    package com.stono;
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    
    public class RabbitConsumer {
        private static final String QUEUE_NAME = "queue_demo";
        private static final String IP_ADDRESS = "192.168.1.103";
        private static final int PORT = 5672;
    
        public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
            Address[] addresses = new Address[]{new Address(IP_ADDRESS, PORT)};
            ConnectionFactory factory = new ConnectionFactory();
            factory.setUsername("root");
            factory.setPassword("root123");
            Connection connection = factory.newConnection(addresses);
            final Channel channel = connection.createChannel();
            channel.basicQos(64);
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("recv message: " + new String(body));
                    try{
                        TimeUnit.SECONDS.sleep(1);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            };
            channel.basicConsume(QUEUE_NAME, consumer);
            TimeUnit.SECONDS.sleep(5);
            channel.close();
            connection.close();
    
        }
    }
  • 相关阅读:
    YARN分析系列之三 -- 从脚本入口分析 ResourceManager的初始化过程
    YARN分析系列之二 -- Hadoop YARN各个自模块说明
    MacOS平台上编译 hadoop 3.1.2 源码
    YARN分析系列之一 -- 总览YARN组件
    WCF入门 (14)
    WCF入门 (13)
    WCF入门(12)
    WCF入门(11)
    WCF入门(10)
    WCF入门(9)
  • 原文地址:https://www.cnblogs.com/stono/p/8831508.html
Copyright © 2011-2022 走看看