Java Code Examples for com.rabbitmq.client.Channel#basicConsume()

The following examples show how to use com.rabbitmq.client.Channel#basicConsume() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProducerExchangeConsumer_Topic.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void consumer3() throws Exception {
	//1、获取连接
       Connection connection =RabbitMqConnectionFactoy.getConnection();
       //2、声明通道
       Channel channel = connection.createChannel();
       //3、声明队列
       channel.queueDeclare(QUEUE_NAME3, false, false, false, null);
       //绑定队列到交换机
       channel.queueBind(QUEUE_NAME3, EXCHANGE_NAME,"test.#");//基数偶数都接收
       //同一时刻服务器只会发送一条消息给消费者(如果设置为N,则当客户端堆积N条消息后服务端不会推送给客户端了)
       //channel.basicQos(1);//每次只从服务器取1个处理
       //4、定义队列的消费者
       DeliverCallback deliverCallback = (consumerTag, delivery) -> {
           String message = new String(delivery.getBody(), "UTF-8");
           System.out.println("-->消费者3号,收到消息,msg :"+message+",header:"+delivery.getProperties().getHeaders().toString());
           channel.basicAck( delivery.getEnvelope().getDeliveryTag(), false);
       };
       channel.basicConsume(QUEUE_NAME3, autoAck, deliverCallback, consumerTag -> { });
}
 
Example 2
Source File: CacheInvalidationSubscriber.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
private void subscribe() {
    log.debug("Global cache invalidation: initializing the subscription");
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(ConfigurationManager.getProviderUrl());
        int port = Integer.parseInt(ConfigurationManager.getProviderPort());
        factory.setPort(port);
        factory.setUsername(ConfigurationManager.getProviderUsername());
        factory.setPassword(ConfigurationManager.getProviderPassword());
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(ConfigurationManager.getTopicName(), "topic");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, ConfigurationManager.getTopicName(), "#");
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
        Thread reciever = new Thread(messageReciever);
        reciever.start();
        log.info("Global cache invalidation is online");
    } catch (Exception e) {
        log.error("Global cache invalidation: Error message broker initialization", e);
    }
}
 
Example 3
Source File: ProducerExchangeConsumer_Fanout.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void consumer1() throws Exception {
	//1、获取连接
       Connection connection =RabbitMqConnectionFactoy.getConnection();
       //2、声明通道
       Channel channel = connection.createChannel();
       //3、声明队列
       channel.queueDeclare(QUEUE_NAME1, false, false, false, null);
       //绑定队列到交换机
       channel.queueBind(QUEUE_NAME1, EXCHANGE_NAME, "");
       //同一时刻服务器只会发送一条消息给消费者(如果设置为N,则当客户端堆积N条消息后服务端不会推送给客户端了)
       //channel.basicQos(1);//每次处理1个
       //4、定义队列的消费者
       //定义消费者
       DefaultConsumer consumer = new DefaultConsumer(channel) {
           @Override
           public void handleDelivery(String consumerTag, Envelope envelope,BasicProperties properties, byte[] body)
                   throws IOException {
               //获取并转成String
               String message = new String(body, "UTF-8");
               System.out.println("-->消费者1号,收到消息,msg :"+message+",header:"+properties.getHeaders().toString());
               channel.basicAck(envelope.getDeliveryTag(), false);
           }
       };
       channel.basicConsume(QUEUE_NAME1, autoAck,consumer);
}
 
Example 4
Source File: ProducerConsumer.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void consumer3() throws Exception {
	//1、获取连接
       Connection connection =RabbitMqConnectionFactoy.getConnection();
       //2、声明通道
       Channel channel = connection.createChannel();
       //3、声明队列
       channel.queueDeclare(QUEUE_NAME, false, false, false, null);
       //同一时刻服务器只会发送一条消息给消费者(如果设置为N,则当客户端堆积N条消息后服务端不会推送给客户端了)
       channel.basicQos(1);//每次只从服务器取1个处理
       //4、定义队列的消费者
       DeliverCallback deliverCallback = (consumerTag, delivery) -> {
           String message = new String(delivery.getBody(), "UTF-8");
           System.out.println("-->消费者3号,收到消息,msg :"+message+",header:"+delivery.getProperties().getHeaders().toString());
           channel.basicAck( delivery.getEnvelope().getDeliveryTag(), false);
       };
       channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, consumerTag -> { });
}
 
Example 5
Source File: ProducerConsumer.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void consumer2() throws Exception {
	//1、获取连接
       Connection connection =RabbitMqConnectionFactoy.getConnection();
       //2、声明通道
       Channel channel = connection.createChannel();
       //3、声明队列
       channel.queueDeclare(QUEUE_NAME, false, false, false, null);
       //同一时刻服务器只会发送一条消息给消费者(如果设置为N,则当客户端堆积N条消息后服务端不会推送给客户端了)
       channel.basicQos(1);//每次只从服务器取1个处理
       //4、定义队列的消费者
       DeliverCallback deliverCallback = (consumerTag, delivery) -> {
           String message = new String(delivery.getBody(), "UTF-8");
           System.out.println("-->消费者2号,收到消息,msg :"+message+",header:"+delivery.getProperties().getHeaders().toString());
           channel.basicAck( delivery.getEnvelope().getDeliveryTag(), false);
       };
       channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, consumerTag -> { });
}
 
Example 6
Source File: MessageSubscriberFactory.java    From Insights with Apache License 2.0 6 votes vote down vote up
public void registerSubscriber(String routingKey, final EngineSubscriberResponseHandler responseHandler) throws Exception {
	Channel channel = connection.createChannel();
	String queueName = routingKey.replace(".", "_");
	channel.queueDeclare(queueName, true, false, false, null);
	channel.queueBind(queueName, MessageConstants.EXCHANGE_NAME, routingKey);
	channel.basicQos(ApplicationConfigProvider.getInstance().getMessageQueue().getPrefetchCount());
	responseHandler.setChannel(channel);
	log.debug("prefetchCount "+ApplicationConfigProvider.getInstance().getMessageQueue().getPrefetchCount() );
	Consumer consumer = new DefaultConsumer(channel) {
		@Override
		public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
				byte[] body) throws IOException {
			responseHandler.handleDelivery(consumerTag, envelope, properties, body);
		}
	};
	channel.basicConsume(queueName, false, routingKey, consumer);
}
 
Example 7
Source File: AMQPConsumer.java    From reactive with Creative Commons Zero v1.0 Universal 6 votes vote down vote up
public void execute(String mode) throws Exception {
	
	Channel channel = AMQPCommon.connect();
	QueueingConsumer consumer = new QueueingConsumer(channel);
	channel.basicConsume("trade.request.q", true, consumer);
	
	if (mode.equalsIgnoreCase("stable")) {lower = 300; upper = 1200;}
	if (mode.equalsIgnoreCase("better")) {lower = 300; upper = 800;}
	if (mode.equalsIgnoreCase("worse")) {lower = 800; upper = 1900;}
	if (mode.equalsIgnoreCase("erratic")) {lower = 200; upper = 5000;}

	while (true) {
		QueueingConsumer.Delivery message = consumer.nextDelivery();
		String msg = new String(message.getBody());
		System.out.println("trade order received: " + msg);
		int response = lower + (int) (Math.random() * (upper-lower));
		System.out.println("trade placed, duration = " + response);
		String newMsg = "response";
		byte[] bmsg = newMsg.getBytes();
		Thread.sleep(response);
		channel.basicPublish("", "trade.response.q", null, bmsg);
	}
}
 
Example 8
Source File: ProducerExchangeConsumer_Fanout.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void consumer2() throws Exception {
	//1、获取连接
       Connection connection =RabbitMqConnectionFactoy.getConnection();
       //2、声明通道
       Channel channel = connection.createChannel();
       //3、声明队列
       channel.queueDeclare(QUEUE_NAME2, false, false, false, null);
       //绑定队列到交换机
       channel.queueBind(QUEUE_NAME2, EXCHANGE_NAME,"");
       //同一时刻服务器只会发送一条消息给消费者(如果设置为N,则当客户端堆积N条消息后服务端不会推送给客户端了)
       //channel.basicQos(1);//每次只从服务器取1个处理
       //4、定义队列的消费者
       DeliverCallback deliverCallback = (consumerTag, delivery) -> {
           String message = new String(delivery.getBody(), "UTF-8");
           System.out.println("-->消费者2号,收到消息,msg :"+message+",header:"+delivery.getProperties().getHeaders().toString());
           channel.basicAck( delivery.getEnvelope().getDeliveryTag(), false);
       };
       channel.basicConsume(QUEUE_NAME2, autoAck, deliverCallback, consumerTag -> { });
}
 
Example 9
Source File: AMQPConfig.java    From reactive with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
public void execute() throws Exception {
	Channel channel = AMQPCommon.connect();
	QueueingConsumer consumer = new QueueingConsumer(channel);
	channel.basicConsume("config.q", true, consumer);

	//cleanup any left over messages before continuing...
	while (true) {
		if (consumer.nextDelivery(1000) == null) break;
		System.out.print(".");
	}
	
	System.out.println("");		
	System.out.println("place_trade_low:     - (-)");
	System.out.println("place_trade_current: 2000 (1000) <==");
	System.out.println("place_trade_high:    - (-)");
	System.out.println("");
	
	while (true) {
		QueueingConsumer.Delivery message = consumer.nextDelivery();
		String msg = new String(message.getBody());
		String[] values = msg.split(",");
		long low = new Long(values[0]);
		long cur = new Long(values[1]);
		long high = new Long(values[2]);
		//message in low,med,high format (duration)
		System.out.println("place_trade_low:     " + low*2 + " (" + low + ")");
		System.out.println("place_trade_current: " + cur*2 + " (" + cur + ") <==");
		System.out.println("place_trade_high:    " + high*2 + " (" + high + ")");
		System.out.println("");
	}			
}
 
Example 10
Source File: NegativeQueueDeleteTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@Test(description = "Test queue auto delete", expectedExceptions = IOException.class)
public void testQueueAutoDelete() throws Exception {
    Channel channel = amqpConnection.createChannel();

    String queueName = "NegativeQueueDeleteTestTestQueueAutoDelete";
    channel.queueDeclare(queueName, false, false, true, null);
    String consumerTag = channel.basicConsume(queueName, new DefaultConsumer(channel));

    channel.basicCancel(consumerTag);

    // This should throw an exception since the queue is auto deleted
    channel.queueDeclarePassive(queueName);
}
 
Example 11
Source File: Recv.java    From java-study with Apache License 2.0 5 votes vote down vote up
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
   // factory.setHost("localhost");
   //  factory.setHost("127.0.0.1");
    factory.setUri("amqp://guest:[email protected]:5672");//获取url
   // 打开连接和创建频道,与发送端一样  
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
 // 声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。  
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
  // 创建队列消费者  
    QueueingConsumer consumer = new QueueingConsumer(channel);
    // 指定消费队列
    channel.basicConsume(QUEUE_NAME, true, consumer);
    while (true) {  //消费者程序运行开着 如果生产者新增了数据会自动获取
    	Thread.sleep(500);
    	 List aa=new ArrayList();
    	 // nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)  
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
      aa.add(message);
      System.out.println("你好吗!"+" [x] Received '" + message + "'"+aa);
    }
   
  }
 
Example 12
Source File: Worker.java    From java-study with Apache License 2.0 5 votes vote down vote up
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    //factory.setHost("localhost");
  	factory.setUri("amqp://guest:[email protected]:5672");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);//queue的持久化需要在声明时指定durable=True
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    //保证在接收端一个消息没有处理完时不会接收另一个消息
   channel.basicQos(1);
  //  channel.basicQos(0, 1, false); //这样RabbitMQ就会使得每个Consumer在同一个时间点最多处理一个Message。换句话说,在接收到该Consumer的ack前,他它不会将新的Message分发给它。
    
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_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);
    }         
  }
 
Example 13
Source File: MessageService.java    From cukes with Apache License 2.0 5 votes vote down vote up
@SneakyThrows({IOException.class, InterruptedException.class})
public MessageWrapper receiveMessage(String queue, int timeoutInSeconds) {
    Channel channel = connectionService.getChannel();
    BlockingQueue<MessageWrapper> result = new ArrayBlockingQueue<MessageWrapper>(1);
    channel.basicConsume(queue, true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            String response = new String(body);
            MessageWrapper messageWrapper = new MessageWrapper(response, properties);
            result.add(messageWrapper);
        }
    });
    return result.poll(timeoutInSeconds, TimeUnit.SECONDS);
}
 
Example 14
Source File: Consumer.java    From code with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost(Constant.ip);
		connectionFactory.setPort(Constant.port);
		connectionFactory.setVirtualHost("/");
		
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		// 这就是一个普通的交换机 和 队列 以及路由
		String exchangeName = "test_dlx_exchange";
		String queueName = "test_dlx_queue";
		String routingKey = "dlx.#";
		
		channel.exchangeDeclare(exchangeName, "topic", true, false, null);

		Map<String, Object> arguments = new HashMap<>();
		arguments.put("x-dead-letter-exchange", "dlx.exchange");
		//这个agruments属性,要设置到声明队列上
		channel.queueDeclare(queueName, true, false, false, arguments);
		channel.queueBind(queueName, exchangeName, routingKey);
		
		//要进行死信队列的声明:
		channel.exchangeDeclare("dlx.exchange", "topic", true, false, null);
		channel.queueDeclare("dlx.queue", true, false, false, null);
		channel.queueBind("dlx.queue", "dlx.exchange", "#");
		
		channel.basicConsume(queueName, true, new MyConsumer(channel));
		
		
	}
 
Example 15
Source File: RabbitEc2LiveTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
protected void doTest(Location loc) throws Exception {
    RabbitBroker rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class));
    rabbit.start(ImmutableList.of(loc));
    EntityAsserts.assertAttributeEqualsEventually(rabbit, RabbitBroker.SERVICE_UP, true);

    byte[] content = "MessageBody".getBytes(Charsets.UTF_8);
    String queue = "queueName";
    Channel producer = null;
    Channel consumer = null;
    try {
        producer = getAmqpChannel(rabbit);
        consumer = getAmqpChannel(rabbit);

        producer.queueDeclare(queue, true, false, false, Maps.<String,Object>newHashMap());
        producer.queueBind(queue, AmqpExchange.DIRECT, queue);
        producer.basicPublish(AmqpExchange.DIRECT, queue, null, content);
        
        QueueingConsumer queueConsumer = new QueueingConsumer(consumer);
        consumer.basicConsume(queue, true, queueConsumer);
    
        QueueingConsumer.Delivery delivery = queueConsumer.nextDelivery();
        assertEquals(delivery.getBody(), content);
    } finally {
        if (producer != null) producer.close();
        if (consumer != null) consumer.close();
    }
}
 
Example 16
Source File: AMQPWorkflowConsumer.java    From reactive with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
public void execute() throws Exception {
	Channel channel = AMQPCommon.connect();
	QueueingConsumer consumer = new QueueingConsumer(channel);
	channel.basicConsume("trade.eq.q", true, consumer);

	while (true) {
		QueueingConsumer.Delivery msg = consumer.nextDelivery();
		String message = new String(msg.getBody());
		System.out.println("received: " + message);
		String[] parts = message.split(",");
		long shares = new Long(parts[2]).longValue();
		Thread.sleep(1000);
	}			
}
 
Example 17
Source File: RabbitMqIOTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteQueue() throws Exception {
  final int maxNumRecords = 1000;
  List<RabbitMqMessage> data =
      RabbitMqTestUtils.generateRecords(maxNumRecords).stream()
          .map(RabbitMqMessage::new)
          .collect(Collectors.toList());
  p.apply(Create.of(data))
      .apply(
          RabbitMqIO.write().withUri("amqp://guest:guest@localhost:" + port).withQueue("TEST"));

  final List<String> received = new ArrayList<>();
  ConnectionFactory connectionFactory = new ConnectionFactory();
  connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
  Connection connection = null;
  Channel channel = null;
  try {
    connection = connectionFactory.newConnection();
    channel = connection.createChannel();
    channel.queueDeclare("TEST", true, false, false, null);
    Consumer consumer = new RabbitMqTestUtils.TestConsumer(channel, received);
    channel.basicConsume("TEST", true, consumer);

    p.run();

    while (received.size() < maxNumRecords) {
      Thread.sleep(500);
    }

    assertEquals(maxNumRecords, received.size());
    for (int i = 0; i < maxNumRecords; i++) {
      assertTrue(received.contains("Test " + i));
    }
  } finally {
    if (channel != null) {
      channel.close();
    }
    if (connection != null) {
      connection.close();
    }
  }
}
 
Example 18
Source File: RabbitMQTestRunner.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
void runPushTest(int numMessages) throws Exception {

        final String message = "hello rabbit mq";

        // producer side
        final Connection producerConnection = connectionFactory.newConnection();
        final Channel producerChannel = producerConnection.createChannel();

        producerChannel.exchangeDeclare(RabbitMQTestConstants.EXCHANGE, "direct", false);
        producerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null);
        producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PUSH, RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH);

        for (int i = 0; i < numMessages; i++) {
            AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
            producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH, false, false, builder.appId("test").build(), message.getBytes());
        }

        producerChannel.close();
        producerConnection.close();

        // consumer side
        final Connection consumerConnection = connectionFactory.newConnection();
        final Channel consumerChannel = consumerConnection.createChannel();
        final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":" + consumerConnection.getPort();

        consumerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null);

        TestConsumer<String> consumer = new TestConsumer<String>(consumerChannel, MessageConverter.FOR_TEST);
        consumerChannel.basicConsume(RabbitMQTestConstants.QUEUE_PUSH, true, consumer);

        List<String> actualMessages = new ArrayList<String>(numMessages);
        for (int i = 0; i < numMessages; i++) {
            actualMessages.add(consumer.getMessage(10, TimeUnit.SECONDS));
        }

        Assert.assertEquals(numMessages, actualMessages.size());
        for (String actualMessage : actualMessages) {
            Assert.assertEquals(message, actualMessage);
        }

        consumerChannel.close();
        consumerConnection.close();

        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
        // Wait till all traces are recorded (consumer traces are recorded from another thread)
        int expectedTraceCountPerMessage = 6;
        awaitAndVerifyTraceCount(verifier, expectedTraceCountPerMessage * numMessages, 5000L);

        verifier.printCache();

        Class<?> producerChannelClass = producerChannel.getClass();
        Method channelBasicPublish = producerChannelClass.getDeclaredMethod("basicPublish", String.class, String.class, boolean.class, boolean.class, AMQP.BasicProperties.class, byte[].class);
        ExpectedTrace channelBasicPublishTrace = Expectations.event(
                RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType
                channelBasicPublish, // method
                null, // rpc
                remoteAddress, // endPoint
                "exchange-" + RabbitMQTestConstants.EXCHANGE, // destinationId
                Expectations.annotation("rabbitmq.exchange", RabbitMQTestConstants.EXCHANGE),
                Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH));
        ExpectedTrace rabbitMqConsumerInvocationTrace = Expectations.root(
                RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType
                "RabbitMQ Consumer Invocation", // method
                "rabbitmq://exchange=" + RabbitMQTestConstants.EXCHANGE, // rpc
                null, // endPoint (collected but API to retrieve local address is not available in all versions, so skip)
                remoteAddress, // remoteAddress
                Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH));
        Class<?> consumerDispatchClass = Class.forName("com.rabbitmq.client.impl.ConsumerDispatcher");
        Method consumerDispatchHandleDelivery = consumerDispatchClass.getDeclaredMethod("handleDelivery", Consumer.class, String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class);
        ExpectedTrace consumerDispatcherHandleDeliveryTrace = Expectations.event(
                RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL,
                consumerDispatchHandleDelivery); // method
        ExpectedTrace asynchronousInvocationTrace = Expectations.event(
                ServiceType.ASYNC.getName(),
                "Asynchronous Invocation");
        Class<?> consumerClass = consumer.getClass();
        Method consumerHandleDelivery = consumerClass.getDeclaredMethod("handleDelivery", String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class);
        ExpectedTrace consumerHandleDeliveryTrace = Expectations.event(
                RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL,
                consumerHandleDelivery);
        Class<?> propagationMarkerClass = PropagationMarker.class;
        Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark");
        ExpectedTrace markTrace = Expectations.event(
                ServiceType.INTERNAL_METHOD.getName(),
                propagationMarkerMark);

        for (int i = 0; i < numMessages; i++) {
            verifier.verifyDiscreteTrace(channelBasicPublishTrace);
            verifier.verifyDiscreteTrace(
                    rabbitMqConsumerInvocationTrace,
                    Expectations.async(
                            consumerDispatcherHandleDeliveryTrace,
                            asynchronousInvocationTrace,
                            consumerHandleDeliveryTrace,
                            markTrace));
        }
        verifier.verifyTraceCount(0);
    }
 
Example 19
Source File: Consumer.java    From code with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	
	
	ConnectionFactory connectionFactory = new ConnectionFactory();
	connectionFactory.setHost(Constant.ip);
	connectionFactory.setPort(Constant.port);
	connectionFactory.setVirtualHost("/");
	
	Connection connection = connectionFactory.newConnection();
	Channel channel = connection.createChannel();
	
	
	String exchangeName = "test_ack_exchange";
	String queueName = "test_ack_queue";
	String routingKey = "ack.#";
	
	channel.exchangeDeclare(exchangeName, "topic", true, false, null);
	channel.queueDeclare(queueName, true, false, false, null);
	channel.queueBind(queueName, exchangeName, routingKey);
	
	// 手工签收 必须要关闭 autoAck = false
	channel.basicConsume(queueName, false, new MyConsumer(channel));
	
	
}
 
Example 20
Source File: Worker.java    From neo4j-mazerunner with Apache License 2.0 4 votes vote down vote up
public void doMain(String[] args) throws Exception {

        CmdLineParser parser = new CmdLineParser(this);

        // if you have a wider console, you could increase the value;
        // here 80 is also the default
        parser.setUsageWidth(80);

        try {
            // parse the arguments.
            parser.parseArgument(args);

            if(sparkMaster == "" || hadoopHdfs == "")
                throw new CmdLineException(parser, "Options required: --hadoop.hdfs <url>, --spark.master <url>");

            ConfigurationLoader.getInstance().setHadoopHdfsUri(hadoopHdfs);
            ConfigurationLoader.getInstance().setSparkHost(sparkMaster);
            ConfigurationLoader.getInstance().setAppName(sparkAppName);
            ConfigurationLoader.getInstance().setExecutorMemory(sparkExecutorMemory);
            ConfigurationLoader.getInstance().setDriverHost(driverHost);
            ConfigurationLoader.getInstance().setRabbitmqNodename(rabbitMqHost);

        } catch( CmdLineException e ) {
            // if there's a problem in the command line,
            // you'll get this exception. this will report
            // an error message.
            System.err.println(e.getMessage());
            System.err.println("java -cp $CLASSPATH [<spark-config-options>] <main-class> [<mazerunner-args>]");
            // print the list of available options
            parser.printUsage(System.err);
            System.err.println();

            // print option sample. This is useful some time
            System.err.println("  Example: java -cp $CLASSPATH org.mazerunner.core.messaging.Worker"+parser.printExample(ALL));

            return;
        }

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename());
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

        channel.basicQos(20);

        // Initialize spark context
        GraphProcessor.initializeSparkContext();

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());

            System.out.println(" [x] Received '" + message + "'");

            // Deserialize message
            Gson gson = new Gson();
            ProcessorMessage processorMessage = gson.fromJson(message, ProcessorMessage.class);

            // Run PageRank
            GraphProcessor.processEdgeList(processorMessage);

            System.out.println(" [x] Done '" + message + "'");
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }