Android Rabbit發送與接收
Android 使用Rabbit需要幾個充要條件
①IP地址
②端口號
③賬號,密碼
④交換機名稱(ExchangeName)
Rabbit遠程配置如圖:
注:交換機的交換類型選擇【fanout】
配置可以參考Android RabbitMQ使用之測試,不用綁定queue隊列!
ok!現在開始代碼部分:、
配置:
private String userName = "dlw" ;
private String passWord = "dlw" ;
private String hostName = "192.168.xx.xxx" ;
private int portNum = 5672 ;
private String exchangeName = "demo" ;
/**
* Rabbit配置
*/
private void setupConnectionFactory(){
factory.setUsername(userName);
factory.setPassword(passWord);
factory.setHost(hostName);
factory.setPort(portNum);
}
發送消息
channel.basicPublish(exchangeName , rountingKey ,null , msg);
exchangeName ----- 交換機名稱 — 已知
rountingKey ------路由鍵 – 自己隨便填寫一個
msg -------- 要發送的消息
/**
* 發消息
*/
private void basicPublish(){
try {
//連接
Connection connection = factory.newConnection() ;
//通道
Channel channel = connection.createChannel() ;
//消息發佈
byte[] msg = "hello girl friend!".getBytes() ;
//rountingKey 自己任意填寫
channel.basicPublish(exchangeName , rountingKey ,null , msg);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
接收消息
basicConsume(String queue, boolean autoAck, String consumerTag, Consumer callback)
queue ------ 隊列名稱 — 這裏我們創建一個臨時隊列(非持久,獨佔,自動刪除)
autoAck - 如果服務器應考慮一旦發送確認的消息,則爲true; 如果服務器應該期望顯式確認,則返回false – false
consumerTag - 客戶端生成的消費者標籤以建立上下文 – 任意寫
callback - 消費者對象的接口 – 我們使用Consumer的子類DefaultConsumer
/**
* 收消息
*/
private void basicConsume(){
try {
//連接
Connection connection = factory.newConnection() ;
//通道
final Channel channel = connection.createChannel() ;
//聲明瞭一個交換和一個服務器命名的隊列,然後將它們綁定在一起。
channel.exchangeDeclare(exchangeName , "fanout" , true) ;
String queueName = channel.queueDeclare().getQueue() ;
Log.e("TAG" , queueName + " :queueName") ;
channel.queueBind(queueName , exchangeName , rountingKey) ;
//實現Consumer的最簡單方法是將便捷類DefaultConsumer子類化。可以在basicConsume 調用上傳遞此子類的對象以設置訂閱:
channel.basicConsume(queueName , false , "administrator" , new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
super.handleDelivery(consumerTag, envelope, properties, body);
String rountingKey = envelope.getRoutingKey() ;//路由密鑰
String contentType = properties.getContentType() ;//contentType字段,如果尚未設置字段,則爲null。
String msg = new String(body,"UTF-8") ;//接收到的消息
long deliveryTag = envelope.getDeliveryTag() ;//交付標記
Log.e("TAG" , rountingKey+":rountingKey") ;
Log.e("TAG" , contentType+":contentType") ;
Log.e("TAG" , msg+":msg") ;
Log.e("TAG" , deliveryTag+":deliveryTag") ;
Log.e("TAG" , consumerTag+":consumerTag") ;
Log.e("TAG" , envelope.getExchange()+":exchangeName") ;
channel.basicAck(deliveryTag , false);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
注
因爲【exchange】的交換類型爲【fanout】(扇形),所以【rountingKey】(路由鍵)的設置會被忽略。我們使用basicPublish發送消息,其實是向該【exchange】裏面的所有【queue】(隊列)發送消息。
因此,如果我們使用這種方式發送與接收信息,那麼你所有的設備都會接受到你某刻發送的消息。
如果我們向裏面exchange裏面的queue發送消息時,exchange裏面沒有queue。那麼是不影響的。