本文整理汇总了Java中com.rabbitmq.client.Channel.basicAck方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.basicAck方法的具体用法?Java Channel.basicAck怎么用?Java Channel.basicAck使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.rabbitmq.client.Channel
的用法示例。
在下文中一共展示了Channel.basicAck方法的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: onMessage
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
/**
* 普通消息监听
*
* @param message 消息实体
* @param channel channel 就是当前的会话通道
* @throws Exception 备注: 手动ack就是在当前channel里面调用basicAsk的方法,并传入当前消息的tagId就可以了。
*/
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
logger.debug("deliveryTag= " + deliveryTag);
try {
logger.info("------消费者处理消息------");
logger.info("receive message" + message.getMessageProperties().getAppId());
logger.info("receive channel" + channel.getChannelNumber() + "----");
// 获取消息
if (null != message.getBody()) {
EventMessage eventMessage = (EventMessage) ObjectAndByteCovertUtil.ByteToObject(message.getBody());
if (null != eventMessage) {
System.out.println(Thread.currentThread().getName() + ":" + TimeUtils.getSysTime("yyyy-MM-dd HH:mm:ss") + ":[下游应用- 消费普通消息]:" + message.getMessageProperties());
// TODO 业务处理
}
}
// 消息的标识,false只确认当前一个消息收到,true确认所有consumer获得的消息(成功消费,消息从队列中删除 )
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
logger.warn("message consume failed: " + e.getMessage());
// ack返回false,requeue-true并重新回到队列
channel.basicNack(deliveryTag, false, true);
}
}
示例3: onMessage
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
/**
* 延迟消息监听并处理
*
* @param message 消息实体
* @param channel channel 就是当前的会话通道
* @throws Exception 备注: 手动ack就是在当前channel里面调用basicAsk的方法,并传入当前消息的tagId就可以了。
*/
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
logger.debug("deliveryTag= " + deliveryTag);
try {
logger.info("[延时消息]" + message.getMessageProperties());
// 获取消息
if (null != message.getBody()) {
EventMessage eventMessage = (EventMessage) ObjectAndByteCovertUtil.ByteToObject(message.getBody());
if (null != eventMessage) {
System.out.println(Thread.currentThread().getName() + ":" + TimeUtils.getSysTime("yyyy-MM-dd HH:mm:ss") + ":[下游应用-消费延时消息]:" + eventMessage.getObject().toString());
// TODO 业务处理
}
}
// 手动确认 - false只确认当前一个消息收到,true确认所有consumer获得的消息
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
logger.warn("message consume failed: " + e.getMessage());
// ack返回false,requeue-true并重新回到队列
channel.basicNack(deliveryTag, false, true);
}
}
示例4: onMessage
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
@Override
public void onMessage(Message message, Channel channel) throws IOException {
System.out.println("----- received" + message.getMessageProperties());
try {
Object msg = messageConverter.fromMessage(message);
if (!appId.equals(message.getMessageProperties().getAppId())){
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
throw new SecurityException("非法应用appId:" + message.getMessageProperties().getAppId());
}
Object service = ctx.getBean(message.getMessageProperties().getHeaders().get("ServiceName").toString());
String serviceMethodName = message.getMessageProperties().getHeaders().get("ServiceMethodName").toString();
Method method = service.getClass().getMethod(serviceMethodName, msg.getClass());
method.invoke(service, msg);
//确认消息成功消费
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
System.out.println("------ err"+ e.getMessage());
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
}
}
示例5: 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();
}
}
示例6: 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);
}
}
示例7: 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);
}
}
示例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();
Thread.sleep(2000);
System.out.println("Trade placed: " + new String(msg.getBody()));
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("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);
}
}
示例10: 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);
}
示例11: ackAction
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
private Consumer<Void> ackAction(final long deliveryTag) {
if (options.autoAckEnabled()) {
return it -> {};
}
return it -> {
final Channel channel = getChannel();
try {
channel.basicAck(deliveryTag, false);
} catch (final IOException e) {
throw new IllegalStateException(String.format("Could not deliver ack for: %d", deliveryTag), e);
}
};
}
示例12: main
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUri("amqp://guest:[email protected]");
factory.setConnectionTimeout(300000);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("my-queue", true, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("my-queue", false, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
if (delivery != null) {
try {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println("Message consumed: " + message);
// Interact with IO
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
} catch (Exception e) {
channel.basicReject(delivery.getEnvelope().getDeliveryTag(), true);
}
}
}
}
示例13: 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);
}
示例14: 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);
}
示例15: recover
import com.rabbitmq.client.Channel; //导入方法依赖的package包/类
@Override
public void recover() {
try {
componentManager.getHeartbeatMQAccessService().start();
stateManager = componentManager.getStateManager();
LOG.info("Start to read messages from MQ ...");
final Channel channel = componentManager.getHeartbeatMQAccessService().getChannel();
final String q = componentManager.getHeartbeatMQAccessService().getQueueName();
while(true) {
GetResponse response = channel.basicGet(q, false);
if(response != null) {
long deliveryTag = response.getEnvelope().getDeliveryTag();
String message = new String(response.getBody(), "UTF-8");
LOG.info("Received msg: deliveryTag=" + deliveryTag + ", message=" + message);
JSONObject result = JSONObject.parseObject(message);
if(result.containsKey(JSONKeys.TYPE)) {
String type = result.getString(JSONKeys.TYPE);
switch(type) {
case ScheduledConstants.HEARTBEAT_TYPE_TASK_PROGRESS:
// {"type":"taskProgress","platform_id":"7fe13e9879314da38bb7abc8b61657bb","tasks":[{"result":{"traceId":"1480402382967","callId":"1","resultCount":"1423","message":"SUCCESS","status":5},"jobId":1,"taskType":1,"taskId":1,"seqNo":1,"status":"SUCCEEDED"}]}
// add flag 'needRecovering' to a tasks response
result.put(ScheduledConstants.NEED_RECOVERING, ScheduledConstants.YES);
needRecoveredTaskQueue.add(result);
channel.basicAck(deliveryTag, false);
break;
}
} else {
channel.basicAck(deliveryTag, false);
}
} else {
break;
}
}
LOG.info("Complete to read MQ messages: q=" + q);
// update Redis statuses
recoverRedisStates();
// recover job/task statuses
processPendingTaskResponses();
} catch (Exception e) {
LOG.error("Recovery failure:", e);
Throwables.propagate(e);
} finally {
componentManager.getHeartbeatMQAccessService().stop();
}
}