當前位置: 首頁>>代碼示例>>Java>>正文


Java DefaultConsumer類代碼示例

本文整理匯總了Java中com.rabbitmq.client.DefaultConsumer的典型用法代碼示例。如果您正苦於以下問題:Java DefaultConsumer類的具體用法?Java DefaultConsumer怎麽用?Java DefaultConsumer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DefaultConsumer類屬於com.rabbitmq.client包,在下文中一共展示了DefaultConsumer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: readMessage

import com.rabbitmq.client.DefaultConsumer; //導入依賴的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

示例2: main

import com.rabbitmq.client.DefaultConsumer; //導入依賴的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

示例3: main

import com.rabbitmq.client.DefaultConsumer; //導入依賴的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

示例4: main

import com.rabbitmq.client.DefaultConsumer; //導入依賴的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

示例5: main

import com.rabbitmq.client.DefaultConsumer; //導入依賴的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

示例6: main

import com.rabbitmq.client.DefaultConsumer; //導入依賴的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

示例7: main

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
/**
 * @param args
 * @throws IOException
 * @throws TimeoutException
 * @date 2017年7月13日 下午3:06:20
 * @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[] { "*.orange.*" };
	// 綁定路由
	for (String routingKey : routingKeys) {
		channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, routingKey);
		System.out.println("ReceiveLogsTopic1 exchange:" + EXCHANGE_NAME_TOPIC + ", queue:" + queueName
				+ ", BindRoutingKey:" + routingKey);
	}
	System.out.println("ReceiveLogsTopic1 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("ReceiveLogsTopic1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
		}
	};
	channel.basicConsume(queueName, true, consumer);

}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:42,代碼來源:ReceiveLogsTopic1.java

示例8: configure

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
/**
 * Configures the rabbitmq queues and the {@code shard-<id>-receive} consumer.
 *
 * Subclasses that want to change how messages are sent/received can override this method to disable the default implementation.
 *
 * @throws IOException if there's an error on {@link Channel#queueDeclare(String, boolean, boolean, boolean, Map)} or {@link Channel#basicConsume(String, boolean, Consumer)}.
 */
protected void configure() throws IOException {
    channel.queueDeclare("shard-" + shardId + "-send", true, false, false, null);
    channel.queueDeclare("shard-" + shardId + "-receive", true, false, false, null);
    channel.queueDeclare("shard-" + shardId + "-available", true, false, false, null);
    channel.queueDeclare("shard-" + shardId + "-unavailable", true, false, false, null);
    channel.basicConsume("shard-" + shardId + "-receive", true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            ClientWebSocketClient c = client.get();
            if(c == null) return;
            JSONObject obj = new JSONObject(new String(body, StandardCharsets.UTF_8));
            if(obj.has("t") && obj.getString("t").equals("gateway-ping-update")) {
                c.getJDA().setPing(obj.getJSONObject("d").getLong("ping"));
                return;
            }
            if(enableRawGatewayEvent)
                c.getJDA().getEventManager().handle(new RawGatewayEvent(c.getJDA(), new JSONObject(obj.toString())));
            c.handleEvent(obj);
        }
    });
}
 
開發者ID:natanbc,項目名稱:discord-bot-gateway,代碼行數:29,代碼來源:GatewayClient.java

示例9: receiveQuickstartMessage

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
/**
 * 接受消息
 */
public void receiveQuickstartMessage() throws IOException, TimeoutException {
    RabbitMQChannel channel = new RabbitMQChannel().channel();
    AMQP.Queue.DeclareOk declareOk = channel.getChannel().queueDeclare(QUICKSTART_QUEUE_NAME, false, false, false, null);
    System.out.println("等待接受隊列【" + QUICKSTART_QUEUE_NAME + "】消息");
    //建立一個消費者 監聽消息的接受
    Consumer consumer = new DefaultConsumer(channel.getChannel()) {
        @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("接受消息:" + message);
        }
    };
    channel.getChannel().basicConsume(QUICKSTART_QUEUE_NAME, true, consumer);
    //channel.close();
}
 
開發者ID:mumudemo,項目名稱:mumu-rabbitmq,代碼行數:21,代碼來源:RabbitMQQuickStart.java

示例10: client

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
/**
 * 調用接口
 *
 * @param message 消息內容 不能為空
 */
public void client(String message) throws IOException, TimeoutException {
    RabbitMQChannel channel = new RabbitMQChannel().channel();
    String replyQueueName = channel.getChannel().queueDeclare().getQueue();
    String corrId = UUID.randomUUID().toString();

    AMQP.BasicProperties props = new AMQP.BasicProperties
            .Builder()
            .correlationId(corrId)
            .replyTo(replyQueueName)
            .build();
    System.out.println("rpc客戶端發送消息:" + message);
    channel.getChannel().basicPublish("", RPC_QUEUE_NAME, props, message.getBytes("UTF-8"));
    channel.getChannel().basicConsume(replyQueueName, true, new DefaultConsumer(channel.getChannel()) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            if (properties.getCorrelationId().equals(corrId)) {
                System.out.println("rpc客戶端收到結果:" + new String(body, "UTF-8") + "\n");
            }
        }
    });
}
 
開發者ID:mumudemo,項目名稱:mumu-rabbitmq,代碼行數:27,代碼來源:RabbitMQRPC.java

示例11: receivePubsubMessage

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
/**
 * 接受消息
 */
public void receivePubsubMessage() throws IOException, TimeoutException {
    RabbitMQChannel channel = new RabbitMQChannel().channel();
    channel.getChannel().exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.getChannel().queueDeclare().getQueue();
    channel.getChannel().queueBind(queueName, EXCHANGE_NAME, "");
    System.out.println("等待接受pusub訂閱【" + EXCHANGE_NAME + "】消息");
    System.out.println("選擇隊列:"+queueName);
    Consumer consumer = new DefaultConsumer(channel.getChannel()) {
        @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("接受消息:" + message + "'");
        }
    };
    channel.getChannel().basicConsume(queueName, true, consumer);
}
 
開發者ID:mumudemo,項目名稱:mumu-rabbitmq,代碼行數:21,代碼來源:RabbitMQPubsub.java

示例12: receiveTopicMessage

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
/**
 * 接受主題消息
 */
public void receiveTopicMessage() throws IOException, TimeoutException {
    RabbitMQChannel channel = new RabbitMQChannel().channel();
    channel.getChannel().exchangeDeclare(TOPIC, "topic");
    String queueName = channel.getChannel().queueDeclare().getQueue();
    channel.getChannel().queueBind(queueName, TOPIC, "bindingKey");
    System.out.println("等待接受topic主題【" + TOPIC + "】消息");
    System.out.println("選擇隊列:" + queueName);
    Consumer consumer = new DefaultConsumer(channel.getChannel()) {
        @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("接受消息:" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.getChannel().basicConsume(queueName, true, consumer);
}
 
開發者ID:mumudemo,項目名稱:mumu-rabbitmq,代碼行數:21,代碼來源:RabbitMQTopic.java

示例13: send

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
@POST
@Path("rabbitmqRecv")
public void send() throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setHost("127.0.0.1");
    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");

    DefaultConsumer consumer = new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {

            System.out.println(properties.getHeaders());
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:28,代碼來源:RabbitMQRecv.java

示例14: GabrielGateway

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
GabrielGateway(int shardId, Channel channel) throws IOException {
    super(shardId, channel);
    channel.queueDeclare("shard-" + shardId + "-getping", false, false, false, null);
    channel.queueDeclare("shard-" + shardId + "-getping-response", false, false, false, null);
    channel.basicConsume("shard-" + shardId + "-getping", true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            channel.basicPublish("", "shard-" + shardId + "-getping-response", null, body);
        }
    });
}
 
開發者ID:natanbc,項目名稱:GabrielBot,代碼行數:12,代碼來源:Gateway.java

示例15: init

import com.rabbitmq.client.DefaultConsumer; //導入依賴的package包/類
public static synchronized void init(Channel channel) throws IOException, TimeoutException {
    if(current != null) {
        throw new IllegalStateException("Already started");
    }
    current = new GatewayInfo("unknown", "unknown", -1, -1, -1, -1, -1, -1, -1);
    channel.queueDeclare("gateway-info", false, false, false, null);
    channel.basicConsume("gateway-info", true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            JSONObject object = new JSONObject(new String(body, StandardCharsets.UTF_8));
            JSONObject ram = object.getJSONObject("ram");
            try {
                current = new GatewayInfo(
                        object.getString("version"),
                        object.getString("jda-version"),
                        object.getDouble("cpu-usage"),
                        object.getInt("thread-count"),
                        object.getLong("uptime"),
                        ram.getLong("used"),
                        ram.getLong("free"),
                        ram.getLong("total"),
                        ram.getLong("max")
                );
            } catch(JSONException e) {
                GabrielBot.LOGGER.error("Error creating GatewayInfo: " + e.getMessage());
            }
        }
    });
}
 
開發者ID:natanbc,項目名稱:GabrielBot,代碼行數:30,代碼來源:GatewayInfo.java


注:本文中的com.rabbitmq.client.DefaultConsumer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。