public static void main(String[] args) throws IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); boolean durable = true; channel.queueDeclare(QUEUE_NAME, durable, false, false, null); System.out.println("[*] waiting for messages.To exit press ctrl+z"); // 公平分发 // 告知RabbitMQ不要同时给一个工作者超过一个任务 // 1个工作者处理完成,发送确认之前不要给它分发一个新的消息 int prefetchcount = 1; channel.basicQos(prefetchcount); QueueingConsumer consumer = new QueueingConsumer(channel); boolean autoAck = false; channel.basicConsume(QUEUE_NAME, false, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); // 模拟工作 doWork(message); System.out.println(" [x] Done"); // 发送确认消息 channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
protected Map<String, Channel> initChannel( Connection connection, int assignedNum, String queueName) throws IOException { Channel consumerChannel = connection.createChannel(); Channel publisherChannel = connection.createChannel(); Map<String, Channel> result = new HashMap<String, Channel>(); if (consumerChannel != null && !consumerChannel.isOpen()) { consumerChannel = connection.createChannel(); } if (publisherChannel != null && !publisherChannel.isOpen()) { publisherChannel = connection.createChannel(); } DecimalFormat formatter = new DecimalFormat("#00.###"); String queueRoutingKey = CONSUMER_ROUTING_KEY + formatter.format(assignedNum); consumerChannel.queueDeclare(queueName, false, false, true, null); consumerChannel.exchangeDeclare(CONSUMER_EXCHANGE_NAME, CONSUMER_EXCHANGE_TYPE, false); consumerChannel.queueBind(queueName, CONSUMER_EXCHANGE_NAME, queueRoutingKey); consumerChannel.basicQos(1); publisherChannel.exchangeDeclare(PUBLISHER_EXCHANGE_NAME, PUBLISHER_EXCHANGE_TYPE, false); result.put("consumer", consumerChannel); result.put("publisher", publisherChannel); return result; }
public static void t1() throws Exception { Channel channel = generateChannel(); // channel.exchangeDeclare(TestAmqp.ExchangerName, "direct", true, false, null); // channel.exchangeDeclare(TestAmqp.FanoutExchangerName, "fanout", true, false, null); channel.exchangeDeclare(TestAmqp.TopicExchangerName, "topic", false, false, null); channel.queueDeclare(TestAmqp.TopicQueueName, false, false, false, null); // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, // TestAmqp.RouteingName); // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "*"); channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "#.*"); // channel.basicPublish(TestAmqp.ExchangerName, TestAmqp.RouteingName, null, "haha".getBytes()); QueueingConsumer queueingConsumer = new QueueingConsumer(channel); channel.basicQos(1); channel.basicConsume(TestAmqp.TopicQueueName, queueingConsumer); while (true) { QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery(); System.out.println(new String(delivery.getBody())); long deliveryTag = delivery.getEnvelope().getDeliveryTag(); System.out.println("start basicAck:" + deliveryTag); // if (true)throw new RuntimeException("xxxxx"); channel.basicAck(deliveryTag, false); } // channel.close(); // connection.close(); }
private void setupAMQP() throws IOException { final int prefetchCount = this.prefetchCount; final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(amqpHost); connectionFactory.setPort(amqpPort); connectionFactory.setUsername(amqpUsername); connectionFactory.setPassword(amqpPassword); connectionFactory.setVirtualHost(amqpVhost); this.amqpConnection = connectionFactory.newConnection(); this.amqpChannel = amqpConnection.createChannel(); log.info("Setting basic.qos prefetch-count to " + prefetchCount); amqpChannel.basicQos(prefetchCount); final Queue.DeclareOk queue = queueDeclaration.declare(amqpChannel); // original line //// String queueName = amqpChannel.queueDeclare().getQueue();from example 4 // amqpChannel.queueDeclare("stormqueue", false, false, false, null);//from example 6 // channel.queueBind("stormqueue", "stormexchange", "error");//from example 6 final String queueName = queue.getQueue(); // original line // final String queueName = "stormqueue"; //added line davidp // log.info("Consuming queue AMQPSpout RPC+JSON!!! " + queueName); this.amqpConsumer = new QueueingConsumer(amqpChannel); this.amqpConsumerTag = amqpChannel.basicConsume(queueName, false /* no auto-ack */, amqpConsumer); }
public static void main(String[] args) throws Exception { // 获取到连接以及mq通道 Connection connection = ConnectionUtil.getConnection(); Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#.#"); // 同一时刻服务器只会发一条消息给消费者 channel.basicQos(1); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成 channel.basicConsume(QUEUE_NAME, false, consumer); // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); Thread.sleep(10); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); final Connection connection = factory.newConnection(); final Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); 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(" [x] Received '" + message + "'"); try { doWork(message); } finally { System.out.println(" [x] Done"); channel.basicAck(envelope.getDeliveryTag(), false); } } }; channel.basicConsume(TASK_QUEUE_NAME, false, consumer); }
public void start() throws IOException { if (channel == null || !channel.isOpen()) { channel = config.createChannel(); channel.addShutdownListener(this); channel.basicQos(getPrefetchCount()); config.declareBrokerConfiguration(channel); consumer = new QueueSubscriber<BasicConsumer<C, T>>(channel, this); channel.basicConsume(queueName, false, consumer); } }
public void StartConsumer() { try { channel = connection.createChannel(); channel.basicQos(1); consumer = new ActualConsumer(channel); consumerTag = channel.basicConsume(Constants.queue, false, consumer); } catch (IOException e) { e.printStackTrace(); } }
@Override protected void connectImpl(TransportConnectionProperties properties) throws IOException { AMQPConnectionProperties connectionProperties = (AMQPConnectionProperties) properties; factory.setThreadFactory(DAEMON_THREAD_FACTORY); factory.setUsername(connectionProperties.getUsername()); factory.setPassword(connectionProperties.getPassword()); factory.setVirtualHost(connectionProperties.getVirtualHost()); factory.setConnectionTimeout(connectionProperties.getConnectionTimeout()); factory.setRequestedHeartbeat(connectionProperties.getHeartbeatInterval()); factory.setAutomaticRecoveryEnabled(connectionProperties.isAutomaticRecoveryEnabled()); connection = factory.newConnection(); channel = connection.createChannel(); channel.basicQos(1); }
public void connect() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); factory.setPort(port); // Authenticate? if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { factory.setUsername(username); factory.setPassword(password); } connection = factory.newConnection(); channel = connection.createChannel(); if (prefetchCount > 0) { channel.basicQos(prefetchCount); LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount); } connection.addShutdownListener( new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { while (true) { try { LOG.error("AMQP connection lost! Trying reconnect in 1 second."); Thread.sleep(1000); connect(); LOG.info("Connected! Re-starting consumer."); run(); LOG.info("Consumer running."); break; } catch (IOException e) { LOG.error("Could not re-connect to AMQP broker.", e); } catch (InterruptedException ignored) { } } } }); }
private void setupAMQP(int prefetchCount) throws IOException { final ConnectionFactory connectionFactory = new ConnectionFactory(); // don't override values if we already have them running if (amqpHost != null) connectionFactory.setHost(amqpHost); if (amqpPort > 0) connectionFactory.setPort(amqpPort); if (amqpUsername != null) connectionFactory.setUsername(amqpUsername); if (amqpPassword != null) connectionFactory.setPassword(amqpPassword); if (amqpVhost != null) connectionFactory.setVirtualHost(amqpVhost); this.amqpConnection = connectionFactory.newConnection(); this.amqpChannel = amqpConnection.createChannel(); log.info("Setting basic.qos prefetch-count to " + prefetchCount); amqpChannel.basicQos(prefetchCount); // will throw an exception here if the exchange does not exist // amqpChannel.exchangeDeclarePassive(amqpExchange); final String queue; // if we passed in a queue name, we expect to be persistent on the // server // therefore we can just connect to it, and assume its there // if it isn't there yet, it will get created, so all the sprouts will // connect to it. if (this.amqpQueueName != null) { this.amqpChannel.queueBind(amqpQueueName, amqpExchange, amqpRoutingKey); queue = amqpQueueName; } // if there is no queue name, just get a server side queue /* * This declares an exclusive, auto-delete, server-named queue. It'll be * deleted when this connection closes, e.g. if the spout worker process * gets restarted. */ else queue = amqpChannel.queueDeclare().getQueue(); amqpChannel.queueBind(queue, amqpExchange, amqpRoutingKey); this.amqpConsumer = new QueueingConsumer(amqpChannel); // start consuming, no auto-acking this.amqpConsumerTag = amqpChannel.basicConsume(queue, false, amqpConsumer); }
public void open() { try { connection = createConnection(); channel = connection.createChannel(); if (prefetchCount > 0) { logger.info("setting basic.qos / prefetch count to " + prefetchCount + " for " + queueName); channel.basicQos(prefetchCount); } // run any declaration prior to queue consumption declarator.execute(channel); consumer = new QueueingConsumer(channel); consumerTag = channel.basicConsume(queueName, isAutoAcking(), consumer); } catch (Exception e) { reset(); logger.error("could not open listener on queue " + queueName); reporter.reportError(e); } }
private void establishConnection() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.0.2.2"); try { connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(DEVICE_ID, true, false, false, null); channel.basicQos(0); consumer = new QueueingConsumer(channel); channel.basicConsume(DEVICE_ID, false, consumer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); }
@Override public void run() { while (true) { Channel ch = null; try { ch = RabbitConnection.getInstance().newChanel(); ch.basicQos(1); AMQP.Queue.DeclareOk q = ch.queueDeclare(); ch.queueBind(q.getQueue(), "amq.direct", routingKey); QueueingConsumer consumer = new QueueingConsumer(ch); ch.basicConsume(q.getQueue(), true, consumer); // Process deliveries while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); Message msg = handler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("msg", message); msg.setData(bundle); handler.sendMessage(msg); } } catch (InterruptedException e) { break; } catch (Exception e1) { try { Thread.sleep(INTERVAL_RECOVERY); // sleep and then try again } catch (InterruptedException e) { break; } } finally { RabbitConnection.getInstance().closeChanel(ch); } } }
/** * @param args * @throws IOException * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws InterruptedException */ public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException, InterruptedException { Properties ps = new Properties(); ps.load( new InputStreamReader( FileUtil.openPropertiesInputStream("log4j.properties"), FileUtil.fileEncode)); PropertyConfigurator.configure(ps); // 创建线程池 HSCloudEventHandler job = new HSCloudEventHandler(job_name, max_thread); // 从RabbitMQ接收消息并处理 try { // 初始化RabbitMQ连接 ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); factory.setUsername(username); factory.setPassword(SecurityHelper.DecryptData(password, securityKey)); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchange, "fanout", durable); DeclareOk ok = channel.queueDeclare(queue, durable, false, false, null); String queueName = ok.getQueue(); channel.queueBind(queueName, exchange, ""); channel.basicQos(1); // 消息分发处理 QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, false, consumer); logger.info( "Begin to receive RabbitMQ event: host[" + host + "] exchange[" + exchange + "] queue[" + queueName + "] max-thread[" + max_thread + "]"); while (true) { Date now = new Date(); if (expirationDate.before(now)) { logger.error("证书失效,系统终止服务。"); System.exit(-1); } QueueingConsumer.Delivery delivery = consumer.nextDelivery(); if (FileUtil.stopProcess()) { logger.info("JobServer receive stop process event and will be end....."); break; } String message = new String(delivery.getBody()); logger.debug(" [" + queue + "] Received '" + message + "'"); // 处理创建虚拟机的消息,创建虚拟机需要分配ip,多以单独放在一个队列里面处理。 if (jobServerType.equals("master")) { if (message.indexOf("createVM_") != -1) { // 根据消息内容判断消息类型 try { // 对创建vm的消息进行处理,分配ip并填充到消息中 message = handleMessage(message); // 创建线程去向openstack请求创建vm job.Handler(message); } catch (Exception e) { // 如果在分配ip过程中出现异常则捕获,把消息中的result:3,置换为result:4,error_info:错误信息 logger.error(e.getMessage(), e); try { // 对消息填充和置换ip分配失败的信息 message = saveGetIpExceptionLog(message, e.getMessage()); // 即使ip分配失败仍然创建线程去向openstack请求创建vm job.Handler(message); } catch (Exception ex) { logger.error(ex.getMessage(), e); } channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); continue; } } channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } else if (jobServerType.equals("slaver")) { // 执行非创建vm的任务 if (message.indexOf("createVM_") == -1) { job.Handler(message); } channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } else if (jobServerType.equals("all")) { try { message = handleMessage(message); job.Handler(message); } catch (Exception e) { logger.error(e.getMessage(), e); try { saveGetIpExceptionLog(message, e.getMessage()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); continue; } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } TimeUnit.MILLISECONDS.sleep(sleepTime); } } catch (Exception e) { logger.error(e.getMessage(), e); } // 退出线程池 job.Shutdown(); while (!job.IsTerminate()) { logger.info("Waiting for JobServer finished!"); } logger.info("JobServer finished!"); System.exit(0); }
@Override public void run() { while (true) { if (closed) { break; } try { connection = connectionFactory.newConnection(rabbitAddresses); channel = connection.createChannel(); } catch (Exception e) { if (!closed) { logger.warn("failed to created a connection / channel", e); } else { continue; } cleanup(0, "failed to connect"); try { Thread.sleep(5000); } catch (InterruptedException e1) { // ignore, if we are closing, we will exit later } } QueueingConsumer consumer = null; // define the queue try { if (rabbitQueueDeclare) { // only declare the queue if we should channel.queueDeclare( rabbitQueue /*queue*/, rabbitQueueDurable /*durable*/, false /*exclusive*/, rabbitQueueAutoDelete /*autoDelete*/, rabbitQueueArgs /*extra args*/); } if (rabbitExchangeDeclare) { // only declare the exchange if we should channel.exchangeDeclare( rabbitExchange /*exchange*/, rabbitExchangeType /*type*/, rabbitExchangeDurable); } channel.basicQos( rabbitQosPrefetchSize /*qos_prefetch_size*/, rabbitQosPrefetchCount /*qos_prefetch_count*/, false); if (rabbitQueueBind) { // only bind queue if we should channel.queueBind( rabbitQueue /*queue*/, rabbitExchange /*exchange*/, rabbitRoutingKey /*routingKey*/); } consumer = new QueueingConsumer(channel); channel.basicConsume(rabbitQueue /*queue*/, false /*noAck*/, consumer); } catch (Exception e) { if (!closed) { logger.warn("failed to create queue [{}]", e, rabbitQueue); } cleanup(0, "failed to create queue"); continue; } // now use the queue to listen for messages while (true) { if (closed) { break; } QueueingConsumer.Delivery task; try { task = consumer.nextDelivery(); } catch (Exception e) { if (!closed) { logger.error("failed to get next message, reconnecting...", e); } cleanup(0, "failed to get message"); break; } if (task != null && task.getBody() != null) { final List<Long> deliveryTags = Lists.newArrayList(); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); try { processBody(task, bulkRequestBuilder); } catch (Exception e) { logger.warn( "failed to parse request for delivery tag [{}], ack'ing...", e, task.getEnvelope().getDeliveryTag()); try { channel.basicAck(task.getEnvelope().getDeliveryTag(), false); } catch (IOException e1) { logger.warn("failed to ack [{}]", e1, task.getEnvelope().getDeliveryTag()); } continue; } deliveryTags.add(task.getEnvelope().getDeliveryTag()); if (bulkRequestBuilder.numberOfActions() < bulkSize) { // try and spin some more of those without timeout, so we have a bigger bulk (bounded // by the bulk size) try { while ((task = consumer.nextDelivery(bulkTimeout.millis())) != null) { try { processBody(task, bulkRequestBuilder); deliveryTags.add(task.getEnvelope().getDeliveryTag()); } catch (Throwable e) { logger.warn( "failed to parse request for delivery tag [{}], ack'ing...", e, task.getEnvelope().getDeliveryTag()); try { channel.basicAck(task.getEnvelope().getDeliveryTag(), false); } catch (Exception e1) { logger.warn( "failed to ack on failure [{}]", e1, task.getEnvelope().getDeliveryTag()); } } if (bulkRequestBuilder.numberOfActions() >= bulkSize) { break; } } } catch (InterruptedException e) { if (closed) { break; } } catch (ShutdownSignalException sse) { logger.warn( "Received a shutdown signal! initiatedByApplication: [{}], hard error: [{}]", sse, sse.isInitiatedByApplication(), sse.isHardError()); if (!closed && sse.isInitiatedByApplication()) { logger.error("failed to get next message, reconnecting...", sse); } cleanup(0, "failed to get message"); break; } } if (logger.isTraceEnabled()) { logger.trace( "executing bulk with [{}] actions", bulkRequestBuilder.numberOfActions()); } // if we have no bulk actions we might have processed custom commands, so ack them if (ordered || bulkRequestBuilder.numberOfActions() == 0) { try { if (bulkRequestBuilder.numberOfActions() > 0) { BulkResponse response = bulkRequestBuilder.execute().actionGet(); if (response.hasFailures()) { // TODO write to exception queue? logger.warn("failed to execute: " + response.buildFailureMessage()); } } } catch (Exception e) { logger.warn("failed to execute bulk", e); } for (Long deliveryTag : deliveryTags) { try { channel.basicAck(deliveryTag, false); } catch (Exception e1) { logger.warn("failed to ack [{}]", e1, deliveryTag); } } } else { if (bulkRequestBuilder.numberOfActions() > 0) { bulkRequestBuilder.execute( new ActionListener<BulkResponse>() { @Override public void onResponse(BulkResponse response) { if (response.hasFailures()) { // TODO write to exception queue? logger.warn("failed to execute: " + response.buildFailureMessage()); } for (Long deliveryTag : deliveryTags) { try { channel.basicAck(deliveryTag, false); } catch (Exception e1) { logger.warn("failed to ack [{}]", e1, deliveryTag); } } } @Override public void onFailure(Throwable e) { logger.warn( "failed to execute bulk for delivery tags [{}], not ack'ing", e, deliveryTags); } }); } } } } } cleanup(0, "closing river"); }
/** {@inheritDoc} */ @Override public SampleResult sample(Entry entry) { SampleResult result = new SampleResult(); result.setSampleLabel(getName()); result.setSuccessful(false); result.setResponseCode("500"); QueueingConsumer consumer; String consumerTag; trace("AMQPConsumer.sample()"); try { initChannel(); consumer = new QueueingConsumer(channel); channel.basicQos(1); // TODO: make prefetchCount configurable? consumerTag = channel.basicConsume(getQueue(), autoAck(), consumer); } catch (IOException ex) { log.error("Failed to initialize channel", ex); return result; } result.setSampleLabel(getTitle()); /* * Perform the sampling */ result.sampleStart(); // Start timing try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(getReceiveTimeoutAsInt()); if (delivery == null) { log.warn("nextDelivery timed out"); return result; } /* * Set up the sample result details */ result.setSamplerData(new String(delivery.getBody())); result.setResponseData("OK", null); result.setDataType(SampleResult.TEXT); result.setResponseCodeOK(); result.setResponseMessage("OK"); result.setSuccessful(true); if (!autoAck()) channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (ShutdownSignalException e) { log.warn("AMQP consumer failed to consume", e); result.setResponseCode("400"); result.setResponseMessage(e.toString()); interrupt(); } catch (ConsumerCancelledException e) { log.warn("AMQP consumer failed to consume", e); result.setResponseCode("300"); result.setResponseMessage(e.toString()); interrupt(); } catch (InterruptedException e) { log.info("interuppted while attempting to consume"); result.setResponseCode("200"); result.setResponseMessage(e.toString()); } catch (IOException e) { log.warn("AMQP consumer failed to consume", e); result.setResponseCode("100"); result.setResponseMessage(e.toString()); } finally { try { channel.basicCancel(consumerTag); } catch (IOException e) { log.error("Couldn't safely cancel the sample's consumer", e); } } result.sampleEnd(); // End timimg trace("AMQPConsumer.sample ended"); return result; }