因爲不可抗因素, 須要給對接方擼一個client的demo。基於比較老的jdk。html
所幸找到了這裏:http://www.rabbitmq.com/releases/rabbitmq-java-clientjava
乾脆拿初版1.4.0開擼,如下demo:api
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差別不太大。安全
有兩疑問:dom
1. ticket幹嗎的:函數
貌似官方的faq裏面有提到:http://rabbitmq.1065348.n5.nabble.com/What-is-ticket-object-and-how-does-it-get-created-td11931.htmlspa
意思初期的不太成熟的一個安全措施的想法, 後面給砍了。 傳0便可。code
2. closecode htm
默認的close函數用了默認的closecode ,和closemessage。blog
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