标签:stat rod blog oid system nbsp lis lag gradle
添加 gradle依赖complie("com.rabbitmq:amqp-client:5.0.0")
Producer:
private static void helloWorld() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel();) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello, World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println("[x] send ‘" + message + "‘"); } catch (Exception ex) { ex.printStackTrace(); } }
Consumer:
private static void helloWorld() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel();) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println("[*] Waiting for message"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerFlag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws UnsupportedEncodingException { String message = new String(body, "UTF-8"); System.out.println("[x] Receive message:‘" + message + "‘"); } }; channel.basicConsume(QUEUE_NAME, consumer); System.out.println("aaaa"); } catch (Exception ex) { ex.printStackTrace(); } }
标签:stat rod blog oid system nbsp lis lag gradle
原文地址:http://www.cnblogs.com/jmbkeyes/p/7631439.html