当前位置: 首页>>代码示例>>Java>>正文


Java Channel.basicConsume方法代码示例

本文整理汇总了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);
}
 
开发者ID:wmr513,项目名称:reactive,代码行数:17,代码来源:AMQPReceiver.java

示例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();
}
 
开发者ID:openzipkin,项目名称:zipkin-reporter-java,代码行数:20,代码来源:RabbitMQSenderTest.java

示例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 + "'");
    }
}
 
开发者ID:javahongxi,项目名称:whatsmars,代码行数:20,代码来源:Consumer.java

示例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++;
	}			
}
 
开发者ID:wmr513,项目名称:reactive,代码行数:19,代码来源:AMQPConsumer.java

示例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));

}
 
开发者ID:aytechnologies,项目名称:heroku-gradle-dropwizard,代码行数:18,代码来源:WorkerApplication.java

示例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);
	}			
}
 
开发者ID:wmr513,项目名称:reactive,代码行数:16,代码来源:AMQPWorkflowProcessor.java

示例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());
		}
	}			
}
 
开发者ID:wmr513,项目名称:reactive,代码行数:23,代码来源:AMQPWorkflowConsumer2.java

示例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();
	}	
}
 
开发者ID:wmr513,项目名称:reactive,代码行数:19,代码来源:AMQPConsumer.java

示例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();
	}			
}
 
开发者ID:wmr513,项目名称:reactive,代码行数:21,代码来源:AMQPInvoicingApp.java

示例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);
	}
}
 
开发者ID:pudoj,项目名称:june.mq,代码行数:40,代码来源:RPCServer.java

示例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);

}
 
开发者ID:pudoj,项目名称:june.mq,代码行数:40,代码来源:ReceiveLogsDirect2.java

示例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);

}
 
开发者ID:pudoj,项目名称:june.mq,代码行数:40,代码来源:ReceiveLogsDirect1.java

示例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);
}
 
开发者ID:pudoj,项目名称:june.mq,代码行数:39,代码来源:Customer.java

示例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);// 队列会自动删除
}
 
开发者ID:pudoj,项目名称:june.mq,代码行数:35,代码来源:ReceiveLogs1.java

示例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);
	}
 
开发者ID:pudoj,项目名称:june.mq,代码行数:40,代码来源:ReceiveLogsTopic2.java


注:本文中的com.rabbitmq.client.Channel.basicConsume方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。