本文整理汇总了Java中com.rabbitmq.client.Consumer类的典型用法代码示例。如果您正苦于以下问题:Java Consumer类的具体用法?Java Consumer怎么用?Java Consumer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Consumer类属于com.rabbitmq.client包,在下文中一共展示了Consumer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.rabbitmq.client.Consumer; //导入依赖的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);
}
示例2: main
import com.rabbitmq.client.Consumer; //导入依赖的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);
}
示例3: main
import com.rabbitmq.client.Consumer; //导入依赖的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);
}
示例4: main
import com.rabbitmq.client.Consumer; //导入依赖的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);// 队列会自动删除
}
示例5: main
import com.rabbitmq.client.Consumer; //导入依赖的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);
}
示例6: main
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月13日 下午3:06:20
* @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[] { "*.orange.*" };
// 绑定路由
for (String routingKey : routingKeys) {
channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, routingKey);
System.out.println("ReceiveLogsTopic1 exchange:" + EXCHANGE_NAME_TOPIC + ", queue:" + queueName
+ ", BindRoutingKey:" + routingKey);
}
System.out.println("ReceiveLogsTopic1 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("ReceiveLogsTopic1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例7: receiveQuickstartMessage
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
/**
* 接受消息
*/
public void receiveQuickstartMessage() throws IOException, TimeoutException {
RabbitMQChannel channel = new RabbitMQChannel().channel();
AMQP.Queue.DeclareOk declareOk = channel.getChannel().queueDeclare(QUICKSTART_QUEUE_NAME, false, false, false, null);
System.out.println("等待接受队列【" + QUICKSTART_QUEUE_NAME + "】消息");
//建立一个消费者 监听消息的接受
Consumer consumer = new DefaultConsumer(channel.getChannel()) {
@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("接受消息:" + message);
}
};
channel.getChannel().basicConsume(QUICKSTART_QUEUE_NAME, true, consumer);
//channel.close();
}
示例8: receivePubsubMessage
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
/**
* 接受消息
*/
public void receivePubsubMessage() throws IOException, TimeoutException {
RabbitMQChannel channel = new RabbitMQChannel().channel();
channel.getChannel().exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.getChannel().queueDeclare().getQueue();
channel.getChannel().queueBind(queueName, EXCHANGE_NAME, "");
System.out.println("等待接受pusub订阅【" + EXCHANGE_NAME + "】消息");
System.out.println("选择队列:"+queueName);
Consumer consumer = new DefaultConsumer(channel.getChannel()) {
@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("接受消息:" + message + "'");
}
};
channel.getChannel().basicConsume(queueName, true, consumer);
}
示例9: receiveTopicMessage
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
/**
* 接受主题消息
*/
public void receiveTopicMessage() throws IOException, TimeoutException {
RabbitMQChannel channel = new RabbitMQChannel().channel();
channel.getChannel().exchangeDeclare(TOPIC, "topic");
String queueName = channel.getChannel().queueDeclare().getQueue();
channel.getChannel().queueBind(queueName, TOPIC, "bindingKey");
System.out.println("等待接受topic主题【" + TOPIC + "】消息");
System.out.println("选择队列:" + queueName);
Consumer consumer = new DefaultConsumer(channel.getChannel()) {
@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("接受消息:" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.getChannel().basicConsume(queueName, true, consumer);
}
示例10: connect
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
protected void connect() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
channel = connection.createChannel();
String queueName = "flowing-retail-" + name;
channel.queueDeclare(queueName, true, false, false, null);
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true); // publish/subscribe model
channel.queueBind(queueName, EXCHANGE_NAME, "*");
System.out.println(" [*] 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(" [x] Received '" + message + "'");
eventHandler.handleEvent(message);
}
};
channel.basicConsume(queueName, true, consumer);
}
示例11: subscribe
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
/**
* Declare an exchange, via an interface that allows the complete set of arguments.
*
* @see com.rabbitmq.client.AMQP.Exchange.Declare
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk <br>
* exchange the name of the exchange <br>
* type the exchange type <br>
* durable true if we are declaring a durable exchange (the exchange will survive a server restart) <br>
* autoDelete true if the server should delete the exchange when it is no longer in use <br>
* internal true if the exchange is internal, i.e. can't be directly published to by a client. <br>
* arguments other properties (construction arguments) for the exchange
* @throws java.io.IOException if an error is encountered
*/
public void subscribe(Exchange exchange, Topic topic, String routingKey) throws Exception {
channel.exchangeDeclare(exchange.exchange(),
exchange.type(),
exchange.durable(),
exchange.autoDelete(),
exchange.autoDelete(),
exchange.properties());
channel.queueDeclare(topic.name(),
topic.durable(),
topic.exclusive(),
topic.autoDelete(), null);
channel.queueBind(topic.name(), exchange.exchange(), routingKey);
final Consumer consumer = createConsumer(channel);
boolean autoAck = true;
channel.basicConsume(topic.name(), autoAck, consumer);
}
示例12: testConnectionFactory
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
@Test
public void testConnectionFactory() throws Exception {
Assert.assertNotNull(connectionFactory1);
Assert.assertNotNull(queue);
RabbitmqConnection connection = connectionFactory1.getConnection();
Assert.assertNotNull(connection);
String queueName = "testing";
Channel channel = connection.createChannel();
channel.queueDeclare(queueName, false, false, false, null);
String message = "Hello World!";
final CountDownLatch counter = new CountDownLatch(1);
Consumer consume = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
BasicProperties properties, byte[] body) throws IOException {
Assert.assertEquals("Hello World!", new String(body));
counter.countDown();
}
};
channel.basicConsume(queueName, true, consume);
channel.basicPublish("", queueName, null, message.getBytes());
counter.await(10, TimeUnit.SECONDS);
Assert.assertEquals(0, counter.getCount());
channel.close();
}
示例13: testStoppingConsumerShutdownConnectionWhenServerHasClosedChannel
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
@Test
public void testStoppingConsumerShutdownConnectionWhenServerHasClosedChannel() throws Exception {
AlreadyClosedException alreadyClosedException = Mockito.mock(AlreadyClosedException.class);
RabbitMQConsumer consumer = new RabbitMQConsumer(endpoint, processor);
Mockito.when(endpoint.createExecutor()).thenReturn(Executors.newFixedThreadPool(3));
Mockito.when(endpoint.getConcurrentConsumers()).thenReturn(1);
Mockito.when(endpoint.connect(Matchers.any(ExecutorService.class))).thenReturn(conn);
Mockito.when(conn.createChannel()).thenReturn(channel);
Mockito.when(channel.basicConsume(anyString(), anyBoolean(), any(Consumer.class))).thenReturn("TAG");
Mockito.when(channel.isOpen()).thenReturn(false);
Mockito.doThrow(alreadyClosedException).when(channel).basicCancel("TAG");
Mockito.doThrow(alreadyClosedException).when(channel).close();
consumer.doStart();
consumer.doStop();
Mockito.verify(conn).close(30 * 1000);
}
示例14: LapinQueueSubscription
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
public LapinQueueSubscription(Stream<QueueSignal> lapinStream, final Subscriber<? super QueueSignal> subscriber,
Queue queue,
boolean bindAckToRequest,
Map<String, Object> consumerArguments,
Subscription dependency,
java.util.function.Consumer<QueueSignal> doOnNext
) {
super(lapinStream, subscriber);
this.queueConfig = queue;
this.bindAckToRequest = bindAckToRequest;
this.consumerArguments = consumerArguments;
this.dependency = dependency;
this.doOnNext = doOnNext != null ? doOnNext : new java.util.function.Consumer<QueueSignal>() {
@Override
public void accept(QueueSignal queueSignal) {
subscriber.onNext(queueSignal);
}
};
}
示例15: testConsumerSingleMessage
import com.rabbitmq.client.Consumer; //导入依赖的package包/类
@Test
public void testConsumerSingleMessage() throws Exception {
TransferQueue<RabbitMessage> messages = new LinkedTransferQueue<>();
Channel channel = mock(Channel.class);
final Consumer consumer = new StreamSetsMessageConsumer(channel, messages);
final Envelope envelope = new Envelope(1L, false, EXCHANGE_NAME, QUEUE_NAME);
executor.submit(new Runnable() {
@Override
public void run() {
try {
consumer.handleDelivery("consumerTag", envelope, null, TEST_MESSAGE_1.getBytes());
} catch (IOException ignored) {
// no op
}
}
});
RabbitMessage message = messages.take();
assertEquals(TEST_MESSAGE_1, new String(message.getBody(), StandardCharsets.UTF_8));
}