本文整理匯總了Java中com.rabbitmq.client.Channel.basicConsume方法的典型用法代碼示例。如果您正苦於以下問題:Java Channel.basicConsume方法的具體用法?Java Channel.basicConsume怎麽用?Java Channel.basicConsume使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.rabbitmq.client.Channel
的用法示例。
在下文中一共展示了Channel.basicConsume方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicQos(1);
channel.basicConsume("trade.eq.q", false, consumer);
int numMsgs = args.length > 0 ? new Integer(args[0]).intValue() : 1;
for (int i=0; i<numMsgs; i++) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
System.out.println("message received: " + new String(msg.getBody()));
Thread.sleep(1000);
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
}
AMQPCommon.close(channel);
}
示例2: readMessage
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
private byte[] readMessage() throws Exception {
final CountDownLatch countDown = new CountDownLatch(1);
final AtomicReference<byte[]> result = new AtomicReference<>();
Channel channel = sender.get().createChannel();
try {
channel.basicConsume(sender.queue(), true, new DefaultConsumer(channel) {
@Override public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
result.set(body);
countDown.countDown();
}
});
countDown.await(5, TimeUnit.SECONDS);
} finally {
channel.close();
}
return result.get();
}
示例3: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
String queueName = "TestQueue";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queueName, false, false, false, null);
System.out.println(" [*] Waiting for messages...");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
}
}
示例4: execute
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public void execute() throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("trade.request.q", true, consumer);
int index = 0;
while (true) {
QueueingConsumer.Delivery message = consumer.nextDelivery();
String msg = new String(message.getBody());
System.out.println("processing trade: " + msg);
String newMsg = "response";
byte[] bmsg = newMsg.getBytes();
Thread.sleep(responseTimes.get(index));
channel.basicPublish("", "trade.response.q", null, bmsg);
index++;
}
}
示例5: run
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
@Override
public void run(AppConfiguration configuration, Environment environment) throws Exception {
SessionFactory sessionFactory = hibernate.getSessionFactory();
MemoDAO memoDAO = new MemoDAO(sessionFactory);
QueueConfig queueConfig = configuration.getQueueConfig();
ConnectionFactory connectionFactory = QueueHelper.getQueue(queueConfig);
Channel channel = connectionFactory.newConnection().createChannel();
channel.exchangeDeclare(queueConfig.getExchangeName(), "direct", true);
channel.queueDeclare(queueConfig.getQueueName(), true, false, false, null);
channel.queueBind(queueConfig.getQueueName(), queueConfig.getExchangeName(), queueConfig.getRoutingKey());
channel.basicConsume(queueConfig.getQueueName(), false, "myConsumerTag", new MemoWorker(memoDAO, channel, sessionFactory));
}
示例6: execute
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public void execute() throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("workflow.q", true, consumer);
while (true) {
QueueingConsumer.Delivery message = consumer.nextDelivery();
String msg = new String(message.getBody());
System.out.println("message received: " + msg);
String newMsg = msg.substring(0, msg.indexOf(" shares"));
byte[] bmsg = newMsg.getBytes();
System.out.println("Trade fixed: " + newMsg);
channel.basicPublish("", "trade.eq.q", null, bmsg);
}
}
示例7: execute
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public void execute() throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("trade.eq.q", true, consumer);
QueueingConsumer.Delivery msg = null;
while (true) {
try {
msg = consumer.nextDelivery();
String message = new String(msg.getBody());
System.out.println("message received: " + message);
String[] parts = message.split(",");
long shares = new Long(parts[2]).longValue();
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("error with trade: " + e.getMessage());
System.out.println("sending to workflow");
channel.basicPublish("", "workflow.q", null, msg.getBody());
}
}
}
示例8: start
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public void start(Connection connection) {
try {
active = true;
Channel channel = connection.createChannel();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicQos(1);
channel.basicConsume("trade.eq.q", false, consumer);
while (active) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
System.out.println("message received: " + new String(msg.getBody()));
Thread.sleep(1000);
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
}
channel.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("sync.q", true, consumer);
displayCache();
while (true) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
String body = new String(msg.getBody());
System.out.println("synchronize message received: " + body);
String[] parts = body.split(",");
String cust = parts[0];
long price = new Long(parts[2]).longValue();
price = (long)(price - (price*.10));
long cost = new Long(cache.get(cust).split(",")[1]).longValue() + price;
long qty = new Long(cache.get(cust).split(",")[0]).longValue() + 1;
cache.put(cust, qty + "," + cost);
displayCache();
}
}
示例10: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @throws InterruptedException
* @throws ConsumerCancelledException
* @throws ShutdownSignalException
*/
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
ConsumerCancelledException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
System.out.println("RPCServer Awating RPC request");
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties replyProps = new AMQP.BasicProperties.Builder().correlationId(props.getCorrelationId())
.build();
String message = new String(delivery.getBody(), "UTF-8");
int n = Integer.parseInt(message);
System.out.println("RPCServer fib(" + message + ")");
String response = "" + fib(n);
channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
示例11: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月13日 下午2:57:32
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明交換器
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
// 獲取匿名隊列名稱
String queueName = channel.queueDeclare().getQueue();
// 根據路由關鍵字進行多重綁定
for (String severity : routingKeys2) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, severity);
System.out.println("ReceiveLogsDirect2 exchange:" + EXCHANGE_NAME_ROUTING + ", queue:" + queueName
+ ", BindRoutingKey:" + severity);
}
System.out.println("ReceiveLogsDirect2 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws UnsupportedEncodingException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsDirect2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例12: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:53:18
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明交換器
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
// 獲取匿名隊列名稱
String queueName = channel.queueDeclare().getQueue();
// 根據路由關鍵字進行綁定
for (String routingKey : routingKeys1) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, routingKey);
System.out.println("ReceiveLogsDirect1 exchange:" + EXCHANGE_NAME_ROUTING + "," + " queue:" + queueName
+ ", BindRoutingKey:" + routingKey);
}
System.out.println("ReceiveLogsDirect1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsDirect1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例13: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月11日 下午5:32:45
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
// 創建連接工廠
ConnectionFactory factory = new ConnectionFactory();
// 設置RabbitMQ地址
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
// 創建一個新的連接
Connection connection = factory.newConnection();
// 創建一個通道
Channel channel = connection.createChannel();
// 聲明要關注的隊列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Customer Waiting Received messages");
// DefaultConsumer類實現了Consumer接口,通過傳入一個頻道,
// 告訴服務器我們需要那個頻道的消息,如果頻道中有消息,就會執行回調函數handleDelivery
Consumer consumer = new DefaultConsumer(channel) {
//envelope主要存放生產者相關信息(比如交換機、路由key等)
//body是消息實體
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("Customer Received '" + message + "'");
}
};
// 自動回複隊列應答 -- RabbitMQ中的消息確認機製
channel.basicConsume(QUEUE_NAME, true, consumer);
}
示例14: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:40:52
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
// 產生一個隨機的隊列名稱
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");// 對隊列進行綁定
System.out.println("ReceiveLogs1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogs1 Received '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);// 隊列會自動刪除
}
示例15: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午3:08:40
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明一個匹配模式的交換器
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
String queueName = channel.queueDeclare().getQueue();
// 路由關鍵字
String[] routingKeys = new String[]{"*.*.rabbit", "lazy.#"};
// 綁定路由關鍵字
for (String bindingKey : routingKeys) {
channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, bindingKey);
System.out.println("ReceiveLogsTopic2 exchange:"+EXCHANGE_NAME_TOPIC+", queue:"+queueName+", BindRoutingKey:" + bindingKey);
}
System.out.println("ReceiveLogsTopic2 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws UnsupportedEncodingException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsTopic2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}