List of usage examples for com.rabbitmq.client Channel queueBind
Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException;
From source file:com.mycompany.bankjsontranslator.Translator.java
public static void main(String[] args) throws IOException { ConnectionFactory listeningFactory = new ConnectionFactory(); listeningFactory.setHost("datdb.cphbusiness.dk"); listeningFactory.setUsername("Dreamteam"); listeningFactory.setPassword("bastian"); ConnectionFactory sendingFactory = new ConnectionFactory(); sendingFactory.setHost("datdb.cphbusiness.dk"); sendingFactory.setUsername("Dreamteam"); sendingFactory.setPassword("bastian"); Connection listeningConnection = listeningFactory.newConnection(); Connection sendingConnection = sendingFactory.newConnection(); final Channel listeningChannel = listeningConnection.createChannel(); final Channel sendingChannel = sendingConnection.createChannel(); final BasicProperties props = new BasicProperties.Builder().replyTo(REPLY_TO_HEADER).build(); listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null); listeningChannel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE_NAME, "CphBusinessJSON"); listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(listeningChannel) { @Override// w ww . j av a 2s . co m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); String[] arr = message.split(","); int dashRemoved = Integer.parseInt(arr[0].replace("-", "")); Result res = new Result(dashRemoved, Integer.parseInt(arr[1]), Double.parseDouble(arr[2]), Integer.parseInt(arr[3])); String result = gson.toJson(res); System.out.println(result); sendingChannel.exchangeDeclare(SENDING_QUEUE_NAME, "fanout"); String test = sendingChannel.queueDeclare().getQueue(); sendingChannel.queueBind(test, SENDING_QUEUE_NAME, ""); sendingChannel.basicPublish(SENDING_QUEUE_NAME, "", props, result.getBytes()); } }; listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer); }
From source file:com.mycompany.bankxmltranslator.Translator.java
public static void main(String[] args) throws IOException { ConnectionFactory listeningFactory = new ConnectionFactory(); listeningFactory.setHost("datdb.cphbusiness.dk"); listeningFactory.setUsername("Dreamteam"); listeningFactory.setPassword("bastian"); ConnectionFactory sendingFactory = new ConnectionFactory(); sendingFactory.setHost("datdb.cphbusiness.dk"); sendingFactory.setUsername("Dreamteam"); sendingFactory.setPassword("bastian"); Connection listeningConnection = listeningFactory.newConnection(); Connection sendingConnection = sendingFactory.newConnection(); final Channel listeningChannel = listeningConnection.createChannel(); final Channel sendingChannel = sendingConnection.createChannel(); listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null); listeningChannel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE_NAME, "CphBusinessXML"); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(listeningChannel) { @Override/*w w w. j a v a 2 s .co m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); String[] arr = message.split(","); // String ssnFix = arr[0].substring(2); String dashRemoved = arr[0].replace("-", ""); Result res = new Result(dashRemoved, Integer.parseInt(arr[1]), Double.parseDouble(arr[2]), theDateAdder(Integer.parseInt(arr[3]))); JAXBContext jaxbContext = JAXBContext.newInstance(Result.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(res, sw); String xmlString = sw.toString(); xmlString = xmlString.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", ""); System.out.println(xmlString); // HANDLE MESSAGE HERE sendingChannel.exchangeDeclare(SENDING_QUEUE_NAME, "fanout"); String test = sendingChannel.queueDeclare().getQueue(); sendingChannel.queueBind(test, SENDING_QUEUE_NAME, ""); sendingChannel.basicPublish("", SENDING_QUEUE_NAME, props, xmlString.getBytes()); } catch (JAXBException ex) { Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex); } } }; listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer); }
From source file:com.mycompany.dreamteamjsontranslator.Translator.java
public static void main(String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("Dreamteam"); factory.setPassword("bastian"); Connection connection = factory.newConnection(); final Channel listeningChannel = connection.createChannel(); final Channel sendingChannel = connection.createChannel(); listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null); listeningChannel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE_NAME, "DreamTeamBankJSON"); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(listeningChannel) { @Override/*from w w w .j a va 2s .c om*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("Hello"); message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); String[] arr = message.split(","); Result res = new Result(arr[0], Integer.parseInt(arr[1]), Double.parseDouble(arr[2]), Integer.parseInt(arr[3])); String result = gson.toJson(res); sendingChannel.queueDeclare(SENDING_QUEUE_NAME, false, false, false, null); sendingChannel.basicPublish("", SENDING_QUEUE_NAME, null, result.getBytes()); } }; listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer); }
From source file:com.mycompany.javateste.queues.pubsub.ReceiveLogs.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); 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(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override//from w w w. ja v a2s .c om 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 + "'"); } }; channel.basicConsume(queueName, true, consumer); }
From source file:com.mycompany.javateste.queues.routing.ReceiveLogsDirect.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); if (argv.length < 1) { System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]"); System.exit(1);/*w ww . j a v a 2 s. c om*/ } for (String severity : argv) { channel.queueBind(queueName, EXCHANGE_NAME, severity); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); 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 '" + envelope.getRoutingKey() + "':'" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); }
From source file:com.mycompany.javateste.queues.topic.ReceiveLogsTopic.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); if (argv.length < 1) { System.err.println("Usage: ReceiveLogsTopic [binding_key]..."); System.exit(1);// www. j av a 2 s. c o m } for (String bindingKey : argv) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); 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 '" + envelope.getRoutingKey() + "':'" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); }
From source file:com.mycompany.loanbroker.requestLoan.java
/** * Web service operation/*from w w w.j av a 2s .c o m*/ */ @WebMethod(operationName = "request") public String request(@WebParam(name = "ssn") String ssn, @WebParam(name = "loanAmount") double loanAmount, @WebParam(name = "loanDuration") int loanDuration) throws IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("Dreamteam"); factory.setPassword("bastian"); Connection connection = factory.newConnection(); Channel sendingchannel = connection.createChannel(); Channel listeningChannel = connection.createChannel(); listeningChannel.exchangeDeclare(EXCHANGE, "direct"); listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null); listeningChannel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE, ssn.replace("-", "")); sendingchannel.queueDeclare(SENDING_QUEUE_NAME, false, false, false, null); message = ssn + "," + loanAmount + "," + loanDuration; sendingchannel.basicPublish("", SENDING_QUEUE_NAME, null, message.getBytes()); sendingchannel.close(); Consumer consumer = new DefaultConsumer(listeningChannel) { @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] The LoanBroker Has Received '" + message + "'"); result = message; } }; listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer); //connection.close(); return result; }
From source file:com.mycompany.net.Run.java
/** * @param args the command line arguments * @throws java.io.IOException/*from ww w.java 2s.c om*/ * @throws java.util.concurrent.TimeoutException */ public static void main(String[] args) throws IOException, TimeoutException { // TODO code application logic here JPA jpa = new JPA(); jpa.initDB(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("systembus"); // RabbitMQ IP Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange AMQP.Queue.DeclareOk result = channel.queueDeclare("", false, true, false, null); String queuename = result.getQueue(); //bind to sensor info channel.queueBind(queuename, "sensors", "gateway.data"); // Binding key is #, this will consume all messages //bind to the dashboard channel.queueBind(queuename, "sensors", "dashboard.request"); //bind to Processing units output channel.queueBind(queuename, "sensors", "database.put"); logger.info(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { String message = new String(body, "UTF-8"); Gson gson = new Gson(); Type type = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> myMap = gson.fromJson(message, type); logger.info(" [x] " + envelope.getRoutingKey() + " - Received '" + message + "'"); String routing_key = envelope.getRoutingKey(); if ("gateway.data".equals(routing_key)) { jpa.saveRawToDB(myMap); } else if ("database.put".equals(routing_key)) { jpa.saveAlertToDB(myMap); } else if ("dashboard.request".equals(routing_key)) { Thread.sleep(1000); jpa.processRequest(myMap, channel); } else logger.error("NOT A VALID MESSAGE!"); } catch (Exception e) { logger.error(e.toString()); } } }; channel.basicConsume(queuename, true, consumer); //channel.close(); //connection.close(); //entityManager.close(); //entityManagerFactory.close(); }
From source file:com.navercorp.pinpoint.plugin.jdk7.rabbitmq.RabbitMQTestRunner.java
License:Apache License
void runPushTest() 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); 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();/*from ww w .j a v a2 s .c o m*/ producerConnection.close(); //comsumer 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); // wait consumer Assert.assertEquals(message, consumer.getMessage(10, TimeUnit.SECONDS)); consumerChannel.close(); consumerConnection.close(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(verifier, 6, 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); verifier.verifyTrace(channelBasicPublishTrace, rabbitMqConsumerInvocationTrace, consumerDispatcherHandleDeliveryTrace, asynchronousInvocationTrace, consumerHandleDeliveryTrace, markTrace); verifier.verifyTraceCount(0); }
From source file:com.navercorp.pinpoint.plugin.jdk7.rabbitmq.RabbitMQTestRunner.java
License:Apache License
void runPullTest() 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_PULL, false, false, false, null); producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PULL, RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL); AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder(); producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL, false, false, builder.appId("test").build(), message.getBytes()); producerChannel.close();/* w w w . j a v a 2 s .c o m*/ producerConnection.close(); //comsumer side final Connection consumerConnection = connectionFactory.newConnection(); final Channel consumerChannel = consumerConnection.createChannel(); final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":" + consumerConnection.getPort(); TestMessagePuller messagePuller = new TestMessagePuller(consumerChannel); Assert.assertEquals(message, messagePuller.pullMessage(MessageConverter.FOR_TEST, RabbitMQTestConstants.QUEUE_PULL, true)); consumerChannel.close(); consumerConnection.close(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(verifier, 5, 5000L); verifier.printCache(); // verify producer traces 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_PULL)); 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_PULL)); Class<?> amqChannelClass = Class.forName("com.rabbitmq.client.impl.AMQChannel"); Method handleCompleteInboundCommand = amqChannelClass.getDeclaredMethod("handleCompleteInboundCommand", AMQCommand.class); ExpectedTrace handleCompleteInboundCommandTrace = Expectations.event( RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, // serviceType handleCompleteInboundCommand); // method verifier.verifyDiscreteTrace(channelBasicPublishTrace, rabbitMqConsumerInvocationTrace, handleCompleteInboundCommandTrace); // verify consumer traces Class<?> consumerChannelClass = consumerChannel.getClass(); Method channelBasicGet = consumerChannelClass.getDeclaredMethod("basicGet", String.class, boolean.class); ExpectedTrace channelBasicGetTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, channelBasicGet); Class<?> propagationMarkerClass = PropagationMarker.class; Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark"); ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark); verifier.verifyDiscreteTrace(channelBasicGetTrace, markTrace); verifier.verifyTraceCount(0); }