List of usage examples for com.rabbitmq.client Channel exchangeDeclare
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type) throws IOException;
From source file:es.devcircus.rabbitmq_examples.rabbitmq_routing.EmitLogDirect.java
License:Open Source License
/** * * @param argv/*from w w w .ja va 2s . co m*/ * @throws Exception */ public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); // factory.setHost("localhost"); factory.setHost("192.168.0.202"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String severity = getSeverity(argv); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "':'" + message + "'"); channel.close(); connection.close(); }
From source file:es.devcircus.rabbitmq_examples.rabbitmq_routing.ReceiveLogsDirect.java
License:Open Source License
/** * * @param argv/*from w w w. j a v a 2s .c om*/ * @throws Exception */ public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); // factory.setHost("localhost"); factory.setHost("192.168.0.202"); 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); } for (String severity : argv) { channel.queueBind(queueName, EXCHANGE_NAME, severity); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] Received '" + routingKey + "':'" + message + "'"); } }
From source file:es.devcircus.rabbitmq_examples.rabbitmq_topics.EmitLogTopic.java
License:Open Source License
/** * /*w ww . ja va 2 s. co m*/ * @param argv */ public static void main(String[] argv) { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String routingKey = getRouting(argv); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:es.devcircus.rabbitmq_examples.rabbitmq_topics.ReceiveLogsTopic.java
License:Open Source License
/** * //from ww w. j av a2 s . c om * @param argv */ public static void main(String[] argv) { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); 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); } for (String bindingKey : argv) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] Received '" + routingKey + "':'" + message + "'"); } } catch (IOException | InterruptedException | ShutdownSignalException | ConsumerCancelledException e) { } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:eu.betaas.rabbitmq.subscriber.SubscriberService.java
License:Apache License
public void startService() { try {/*w w w. ja v a2s .com*/ log.info("#Starting queue #"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection; log.info("#Starting #"); connection = factory.newConnection(); log.info("#Starting connection#"); Channel channel = connection.createChannel(); channel.exchangeDeclare(ename, mode); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ename, bussubkey); log.info("#Starting connection on queue #"); consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); log.info("#Running #"); run(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ShutdownSignalException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ConsumerCancelledException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:loanbroker.Aggregator.java
private void send(entity.Message m, String routingKey) throws IOException, TimeoutException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName);//from w ww . j av a 2 s.co m factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ExchangeName.GLOBAL, "direct"); //creating LoanResponse object LoanResponse lp = new LoanResponse(parseInt(m.getSsn()), m.getCreditScore(), m.getLoanDuration(), ""); Gson gson = new GsonBuilder().create(); String fm = gson.toJson(lp); BasicProperties props = new BasicProperties.Builder().build(); channel.basicPublish(ExchangeName.GLOBAL, routingKey, props, fm.getBytes()); System.out.println(" [x] Sent '" + ExchangeName.GLOBAL + routingKey + "':'" + fm + "'"); channel.close(); connection.close(); }
From source file:loanbroker.Aggregator.java
public void reciveFromRecieptList(Hashtable<String, Message> messagesFromBankList) throws IOException, TimeoutException, Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName);/*from w ww. j a va2s. com*/ factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ExchangeName.GLOBAL, "direct"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.RecipientListToAggregator); System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.RecipientListToAggregator + ".. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String m = new String(body, "UTF-8"); // System.out.println("reciveFromRecieptList" + m); String p = properties.getCorrelationId(); if (p != null) { //send to translator Gson g = new Gson(); Message fm = g.fromJson(m, Message.class); if (fm.getBanks() != null) { Message k = new Message(fm.getSsn(), fm.getCreditScore(), fm.getLoanAmount(), fm.getLoanDuration()); k.setBanks(fm.getBanks()); messagesFromBankList.put(p, k); } System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'"); // System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'"); } else { System.out.println("No correlationId"); } } }; channel.basicConsume(queueName, true, consumer); }
From source file:loanbroker.GetCreditScore.java
void recive() throws IOException, TimeoutException, InterruptedException, Exception { //setting the connection to the RabbitMQ server ConnectionFactory connfac = new ConnectionFactory(); connfac.setHost(hostName);/*from ww w. ja v a 2 s . co m*/ connfac.setUsername("student"); connfac.setPassword("cph"); //make the connection Connection conn = connfac.newConnection(); //make the channel for messaging Channel chan = conn.createChannel(); //Declare a queue chan.exchangeDeclare(ExchangeName.OUTPUT_LOAN_REQUEST, "fanout"); String queueName = chan.queueDeclare().getQueue(); chan.queueBind(queueName, ExchangeName.OUTPUT_LOAN_REQUEST, ""); System.out.println( " [*] Waiting for messages on " + ExchangeName.OUTPUT_LOAN_REQUEST + ". To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(chan); chan.basicConsume(queueName, true, consumer); //start polling messages while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String m = new String(delivery.getBody()); System.out.println(" [x] Received '" + m + "'"); Gson gson = new GsonBuilder().create(); Message fm = gson.fromJson(m, Message.class); int creditScore = creditScore(fm.getSsn()); fm.setCreditScore(creditScore); fm.setSsn(fm.getSsn().replace("-", "")); send(fm); } }
From source file:loanbroker.GetCreditScore.java
public void send(entity.Message creditScoreMessage) throws IOException, TimeoutException, Exception { ConnectionFactory connfac = new ConnectionFactory(); connfac.setHost(hostName);/* w w w . j a v a 2 s. co m*/ connfac.setPort(5672); connfac.setUsername("student"); connfac.setPassword("cph"); Gson gson = new GsonBuilder().create(); String fm = gson.toJson(creditScoreMessage); Connection connection = connfac.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ExchangeName.OUTPUT_GET_CREDITSCORE, "fanout"); channel.basicPublish(ExchangeName.OUTPUT_GET_CREDITSCORE, "", null, fm.getBytes()); System.out.println(" [x] Sent '" + fm.toString() + "'"); channel.close(); connection.close(); }
From source file:loanbroker.normalizer.CallTeachersJsonBank.java
public static void main(String[] args) { Gson gson = new Gson(); DtoJsonBank jsonBank = null;/* www. j av a 2 s . c om*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); //reply String corrId = java.util.UUID.randomUUID().toString(); BasicProperties props = new BasicProperties.Builder().correlationId("test1122").replyTo(replyQueueName) .build(); String message = gson.toJson(new DtoJsonBank("1605789787", 598, 10.0, "")); channel.basicPublish(EXCHANGE_NAME, "", props, message.getBytes()); channel.close(); connection.close(); } catch (IOException | TimeoutException e) { e.printStackTrace(); } }