本文整理汇总了Java中com.rabbitmq.client.Channel.basicQos方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.basicQos方法的具体用法?Java Channel.basicQos怎么用?Java Channel.basicQos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.rabbitmq.client.Channel
的用法示例。
在下文中一共展示了Channel.basicQos方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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();
}
}
示例3: openChannel
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
/**
* Open channel
*/
private Channel openChannel(Connection conn) throws IOException {
log.trace("Creating channel...");
Channel channel = conn.createChannel();
log.debug("Created channel: {}", channel);
// setup the basicQos
if (consumer.getEndpoint().isPrefetchEnabled()) {
channel.basicQos(consumer.getEndpoint().getPrefetchSize(), consumer.getEndpoint().getPrefetchCount(),
consumer.getEndpoint().isPrefetchGlobal());
}
// This really only needs to be called on the first consumer or on
// reconnections.
if (consumer.getEndpoint().isDeclare()) {
consumer.getEndpoint().declareExchangeAndQueue(channel);
}
return channel;
}
示例4: 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);
}
}
示例5: initChannel
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
private void initChannel(Channel channel) throws IOException {
channel.basicQos(1);
// this.channel.exchangeDeclare(this.exchange, TOPIC);
Map<String, Object> args = new HashMap<>();
args.put("x-expires", 180000); // Three minutes
channel.queueDeclare(QUEUE_NAME, true, true, true, args);
channel.queueBind(QUEUE_NAME, NOVA_EXCHANGE, ROUTING_KEY);
channel.queueBind(QUEUE_NAME, NEUTRON_EXCHANGE, ROUTING_KEY);
channel.queueBind(QUEUE_NAME, KEYSTONE_EXCHANGE, ROUTING_KEY);
channel.queueBind(this.queue, this.exchange, this.routingKey);
}
示例6: dispatchMessages
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
public void dispatchMessages() throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicQos(1);
channel.basicConsume("trade.eq.q", false, consumer);
while (true) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
new Thread(new POJOThreadProcessor(
this, new String(msg.getBody()))).start();
numThreads++;
System.out.println("Threads: " + numThreads);
}
}
示例7: 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);
while (true) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
Thread.sleep(2000);
System.out.println("Trade placed: " + new String(msg.getBody()));
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
}
}
示例8: 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);
while (true) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
System.out.println("message received: " + new String(msg.getBody()));
Thread.sleep(2000);
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
}
}
示例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.basicQos(1);
channel.basicConsume(args[0], false, consumer);
while (true) {
QueueingConsumer.Delivery msg = consumer.nextDelivery(1000);
if (msg == null) break;
System.out.println("message received: " + new String(msg.getBody()));
Thread.sleep(100);
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
}
System.exit(0);
}
示例10: createListenerContainer
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
@Override
protected Connection createListenerContainer() throws Exception {
log.debug("Creating connection");
Connection conn = endpoint.connect(executorService);
log.debug("Creating channel");
Channel channel = conn.createChannel();
// setup the basicQos
if (endpoint.isPrefetchEnabled()) {
channel.basicQos(endpoint.getPrefetchSize(), endpoint.getPrefetchCount(),
endpoint.isPrefetchGlobal());
}
//Let the server pick a random name for us
DeclareOk result = channel.queueDeclare();
log.info("Using temporary queue name: {}", result.getQueue());
setReplyTo(result.getQueue());
//TODO check for the RabbitMQConstants.EXCHANGE_NAME header
channel.queueBind(getReplyTo(), endpoint.getExchangeName(), getReplyTo());
consumer = new RabbitConsumer(this, channel);
consumer.start();
return conn;
}
示例11: main
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月11日 下午5:55:38
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
final ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
System.out.println("Worker1 Waiting for messages");
// 每次从队列获取的数量
//channel.basicQos(1);保证一次只分发一个
channel.basicQos(1);
final 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("Worker1 Received '" + message + "'");
try {
//throw new Exception();
doWork(message);
} catch (Exception e) {
channel.abort();
} finally {
System.out.println("Worker1 Done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//autoAck是否自动回复,
//如果为true的话,每次生产者只要发送信息就会从内存中删除,
//那么如果消费者程序异常退出,那么就无法获取数据,
//我们当然是不希望出现这样的情况,所以才去手动回复,
//每当消费者收到并处理信息然后在通知生成者。
//最后从队列中删除这条信息。
//如果消费者异常退出,如果还有其他消费者,
//那么就会把队列中的消息发送给其他消费者,
//如果没有,等消费者启动时候再次发送。
boolean autoAck = false;
// 消息消费完成确认
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
}
示例12: main
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月11日 下午5:55:38
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
final ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
System.out.println("Worker2 Waiting for messages");
// 每次从队列获取的数量
//channel.basicQos(1);保证一次只分发一个
channel.basicQos(1);
final 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("Worker2 Received '" + message + "'");
try {
//throw new Exception();
doWork(message);
} catch (Exception e) {
channel.abort();
} finally {
System.out.println("Worker2 Done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//autoAck是否自动回复,
//如果为true的话,每次生产者只要发送信息就会从内存中删除,
//那么如果消费者程序异常退出,那么就无法获取数据,
//我们当然是不希望出现这样的情况,所以才去手动回复,
//每当消费者收到并处理信息然后在通知生成者。
//最后从队列中删除这条信息。
//如果消费者异常退出,如果还有其他消费者,
//那么就会把队列中的消息发送给其他消费者,
//如果没有,等消费者启动时候再次发送。
boolean autoAck = false;
// 消息消费完成确认
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
}