zoukankan      html  css  js  c++  java
  • rabbitmq

    由于不可抗因素, 需要给对接方撸一个client的demo。基于比较老的jdk。

    所幸找到了这里:http://www.rabbitmq.com/releases/rabbitmq-java-client

    干脆拿第一版1.4.0开撸,以下demo:

    import java.io.IOException;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.MessageProperties;
    import com.rabbitmq.client.ConnectionParameters;
    
    public class NewTask {
    
      private static final String TASK_QUEUE_NAME = "your_xxx_queue";
    
      public static void main(String[] argv)
                          throws java.io.IOException {
        String hostName ="your.domain.name";
        int portNumber = 5672;//AMQP.PROTOCOL.PORT
        int ticket =  0;
        ConnectionParameters params = new ConnectionParameters();
        params.setUsername("username_xxx");
        params.setPassword("password_xxx");
        params.setVirtualHost("virutalhost_xxxx");
        params.setRequestedHeartbeat(0);//TODO:
        ConnectionFactory factory = new ConnectionFactory(params);
        Connection connection = factory.newConnection(hostName, portNumber);
    
        Channel channel = connection.createChannel();
    
        channel.queueDeclare(ticket,TASK_QUEUE_NAME, false, true, false, false, null);
        /*
        queueDeclare(int ticket,
                                      java.lang.String queue,
                                      boolean passive,
                                      boolean durable,
                                      boolean exclusive,
                                      boolean autoDelete,
                                      java.util.Map<java.lang.String,java.lang.Object> arguments)
        */
    
        String message = "hello";
    
        //http://www.rabbitmq.com/releases/rabbitmq-java-client/v1.4.0/rabbitmq-java-client-javadoc-1.4.0/com/rabbitmq/client/Channel.html#basicPublish
        channel.basicPublish(ticket, "", TASK_QUEUE_NAME,
                MessageProperties.PERSISTENT_TEXT_PLAIN,
                message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        int closeCode = 200;
        String closeMessage="job done.";
        channel.close(closeCode, closeMessage);
        connection.close();
      }      
      //...
    }

    基本是看着api文档改的,不同版本的api还真是出入不小(特别httpclient_xxx.jar),还好rabbitmq的java client差异不太大。

    有两疑问:

    1. ticket干嘛的:

    貌似官方的faq里面有提到:http://rabbitmq.1065348.n5.nabble.com/What-is-ticket-object-and-how-does-it-get-created-td11931.html

    意思初期的不太成熟的一个安全措施的想法, 后面给砍了。 传0即可。

    2. closecode 

    默认的close函数用了默认的closecode ,和closemessage。

    https://www.rabbitmq.com/releases/rabbitmq-java-client/current-javadoc/com/rabbitmq/client/Connection.html#close--

    因此低版本中,需要显示的传这两个参数就好了:

    closecode:200, 参考:https://www.rabbitmq.com/releases/rabbitmq-java-client/current-javadoc/constant-values.html#com.rabbitmq.client.AMQP.REPLY_SUCCESS

    closemessage: 'OK'

    来源(为了更好的排版阅读):http://www.cnblogs.com/Tommy-Yu/p/6402672.html

  • 相关阅读:
    Python rindex()方法
    Python rfind()方法
    Python replace()方法
    服务器技术综述(三)
    服务器技术综述(二)
    服务器技术综述(一)
    TensorRT-优化-原理
    TensorRT-安装-使用
    TensorRT 加速性能分析
    GPU加速:宽深度推理
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/6402672.html
Copyright © 2011-2022 走看看