???????????????????????????java?????????????php????rabbitmq????????????????????????
???????rabbitmq??????????????php?????????????????С?
?????????????rabbitmq-client?????java??jar??(spring??????????????)
???????????coding????????????
package com.eelly.imagesearch.common;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;
public class RabbitMqControll {
/**
* ???RabbitMq?е?洢???
*
* @param queue_name ??????
* @param exchange_name ????????
* @param route_key ???????route_key
* @param durable ??????
*/
public void readRabbitMqInfo (String queue_name??
String exchange_name?? String route_key?? boolean durable)
{
ConnectionFactory factory = new ConnectionFactory();
// ???÷?????ip
factory.setHost("172.18.107.66");
// ????rabbitmq?????????е???
factory.setPort(5672);
// ????rabbitmq?????????????
factory.setUsername("guest");
// ????rabbitmq?????????????????
factory.setPassword("guest");
// ????rabbitmq???????????(???????)
factory.setVirtualHost("/");
try {
// ????????????
Connection connection = factory.newConnection();
// ???????
Channel channel = connection.createChannel();
// ??????????(?????????????????php??????)
channel.exchangeDeclare(exchange_name?? "direct"?? durable);
// ???????????(?????????????????php??????)
channel.queueDeclare(queue_name?? durable?? false?? true?? null);
// ?????????(?????????????????php??????)
channel.queueBind(queue_name?? exchange_name?? route_key);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
// basicConsume??????
/*channel.basicQos(1);//??????????
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queue_name?? false?? consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
// ??????????????
channel.basicAck(delivery.getEnvelope().getDeliveryTag()?? false);
}*/
// basicGet??????
while (true)
{
// get???????????
GetResponse res=channel.basicGet(queue_name?? false);
if (res != null && res.getMessageCount() >= 0)
{
System.out.println(res.getMessageCount());
String message = "";
message = new String(res.getBody());
channel.basicAck(res.getEnvelope().getDeliveryTag()?? false);
System.out.println(" [x] Received '" + message + "'");
}
else
{
System.out.println("???????????п???????????");
break;
}
}
channel.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
???????????????У?????????????
????1.??????????????????????????????????????????php??????????????????2?????????д????
????2.??????????????????????basicConsume????????????basicGet??????(?????м??Ч??????????У???????????????????????while??????????е????)