List of usage examples for com.rabbitmq.client Channel exchangeDeclare
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type) throws IOException;
From source file:loanbroker.normalizer.NormalizerOurJsonBank.java
public static void main(String[] args) { try {/*w w w. j a v a 2s.com*/ 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"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, ""); //channel.queueDeclare(RPC_QUEUE_NAME,false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); //producer Channel channelOutput = connection.createChannel(); channelOutput.exchangeDeclare(ExchangeName.GLOBAL, "direct"); String queueNameProducer = channelOutput.queueDeclare().getQueue(); channelOutput.queueBind(queueNameProducer, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator); LoanResponse loanResponse; while (true) { System.out.println("Reading"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId()); String message = new String(delivery.getBody()); JSONObject jsonObj = new JSONObject(message); loanResponse = new LoanResponse(jsonObj.getInt("ssn"), Math.ceil(jsonObj.getDouble("interestRate")), "Our Json Bank", delivery.getProperties().getCorrelationId()); System.out.println("renter: " + loanResponse.getInterestRate()); System.out.println("ssn: " + loanResponse.getSsn()); System.out.println("bank : " + loanResponse.getBank()); System.out.println("JSON:" + loanResponse); System.out.println("TOstring:" + jsonObj.toString()); Gson g = new Gson(); String fm = g.toJson(loanResponse); channelOutput.basicPublish(ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator, null, fm.getBytes()); } } catch (IOException | TimeoutException | InterruptedException e) { e.printStackTrace(); } }
From source file:loanbroker.normalizer.NormalizerOurSoapXmlBank.java
public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException { //Connection/*from w ww . j av a 2 s. c o m*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); //Consumer channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String queueName = channel.queueDeclare().getQueue(); //s channel.queueBind(queueName, EXCHANGE_NAME, "OurSoapXmlBank"); channel.queueBind(queueName, EXCHANGE_NAME, ""); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); //Producer Channel channelOutput = connection.createChannel(); channelOutput.exchangeDeclare("TeamFirebug", "direct"); LoanResponse loanResponse; DtoOurSoapXmlBank dtoOurSoapXmlBank; while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message), DtoOurSoapXmlBank.class); loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(), dtoOurSoapXmlBank.getInterestRate(), "Our Soap Xml bank", delivery.getProperties().getCorrelationId()); // loanResponse.setBank(routingKey); System.out.println("renter: " + loanResponse.getInterestRate()); System.out.println("ssn: " + loanResponse.getSsn()); System.out.println("bank : " + loanResponse.getBank()); JSONObject jsonObj = new JSONObject(loanResponse); // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes()); channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null, jsonObj.toString().getBytes()); } }
From source file:loanbroker.normalizer.NormalizerTeachersJsonBank.java
public static void main(String[] args) { try {/*from ww w . java 2 s. co m*/ 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"); //String queueName = channel.queueDeclare().getQueue(); //channel.queueBind(queueName, EXCHANGE_NAME, ""); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, true, consumer); //producer Channel channelOutput = connection.createChannel(); channelOutput.exchangeDeclare(ExchangeName.GLOBAL, "direct"); String queueName = channelOutput.queueDeclare().getQueue(); channelOutput.queueBind(queueName, ExchangeName.GLOBAL, "normalizerToAggregator"); LoanResponse loanResponse; while (true) { System.out.println("Reading"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId()); String message = new String(delivery.getBody()); JSONObject jsonObj = new JSONObject(message); loanResponse = new LoanResponse(jsonObj.getInt("ssn"), jsonObj.getDouble("interestRate"), "Teachers Json Bank", delivery.getProperties().getCorrelationId()); System.out.println("renter: " + loanResponse.getInterestRate()); System.out.println("ssn: " + loanResponse.getSsn()); System.out.println("bank : " + loanResponse.getBank()); // System.out.println(" [x] Received '" + message + "'"); System.out.println("JSON:" + loanResponse); System.out.println("TOstring:" + jsonObj.toString()); Gson g = new Gson(); String fm = g.toJson(loanResponse); channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null, fm.getBytes()); } } catch (IOException | TimeoutException | InterruptedException e) { e.printStackTrace(); } }
From source file:loanbroker.normalizer.NormalizerTeachersXmlBank.java
public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException { //Connection/*from w ww . jav a 2 s .c om*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); //Consumer channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); //String queueName = channel.queueDeclare().getQueue(); //s channel.queueBind(queueName, EXCHANGE_NAME, "OurSoapXmlBank"); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, true, consumer); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); //Producer Channel channelOutput = connection.createChannel(); channelOutput.exchangeDeclare("TeamFirebug", "direct"); while (true) { System.out.println("Reading"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); DtoTeachersXmlBank dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message), DtoTeachersXmlBank.class); LoanResponse loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(), dtoOurSoapXmlBank.getInterestRate(), "Teachers Xml Bank", delivery.getProperties().getCorrelationId()); // loanResponse.setBank(routingKey); System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId()); System.out.println("renter: " + loanResponse.getInterestRate()); System.out.println("ssn: " + loanResponse.getSsn()); System.out.println("bank : " + loanResponse.getBank()); JSONObject jsonObj = new JSONObject(loanResponse); System.out.println("JSON:" + jsonObj); // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes()); channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null, jsonObj.toString().getBytes()); delivery = null; } }
From source file:loanbroker.RecipientList.java
public static void main(String[] args) throws Exception { //make correlationId for Aggregator //rabbit connect final String exchangeName = ExchangeName.OUTPUT_GET_BANKS; RabbitConnection rabbitConnection = new RabbitConnection(); Channel channel = rabbitConnection.makeConnection(); channel.exchangeDeclare(exchangeName, "fanout"); String queueName = channel.queueDeclare().getQueue(); //channel.queueBind(queueName, exchangeName, RoutingKeys.RecipientListInput); channel.queueBind(queueName, exchangeName, ""); //get banks from queue. "Get banks" component QueueingConsumer consumer = new QueueingConsumer(channel) { String corrId = java.util.UUID.randomUUID().toString(); @Override//from w w w .j ava 2 s . c om public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); //send to Aggregator first sendMessage(ExchangeName.GLOBAL, RoutingKeys.RecipientListToAggregator, message, corrId); //send to translator Message request = getFromJson(message); if (request.getBanks() != null) { for (Bank bank : request.getBanks()) { sendMessage(ExchangeName.GLOBAL, bank.getRoutingKey(), message, corrId); //remember that the component "Get banks" must choose which banks we need to send to(according to credit score) } } } }; channel.basicConsume(queueName, true, consumer); }
From source file:loanbroker.RecipientList.java
private static void sendMessage(String exchangeName, String routingKey, String msg, String corrId) { RabbitConnection rabbitConnection = new RabbitConnection(); Channel channel = rabbitConnection.makeConnection(); try {//from w ww. j a v a2 s. co m //set correlationId for Aggregator AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().correlationId(corrId).build(); channel.exchangeDeclare(exchangeName, "direct"); channel.basicPublish(exchangeName, routingKey, props, msg.getBytes("UTF-8")); rabbitConnection.closeChannelAndConnection(); System.out.println(" [x] Sent :" + routingKey + " " + msg + ""); } catch (IOException ex) { System.out.println("Error in RecipientList class - sendToTranslator()"); System.out.println(ex.getStackTrace()); } }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * Create a new subscriber service./*from w w w. ja va 2 s. c om*/ * * @param baseSource the feed base source * @param selector the selector on the feed source (can be null) * @param feedWorker the feed message worker * @return the new subscriber service */ @Override public MomAkkaService subscriberService(final String baseSource, String selector, AppMsgWorker feedWorker) { MomAkkaService ret = null; ActorRef subsActor; MomConsumer consumer; final Connection connection = ((Client) super.getMomClient()).getConnection(); if (selector == null || selector.equals("")) selector = "#"; if (connection != null && connection.isOpen()) { subsActor = super.getMomClient().getActorSystem().actorOf(MsgSubsActor.props(feedWorker), baseSource + "." + ((selector.equals("#")) ? "all" : selector) + "_msgWorker"); final ActorRef runnableSubsActor = subsActor; final String select = selector; final Client cli = ((Client) super.getMomClient()); consumer = new MomConsumer() { private boolean isRunning = false; @Override public void run() { Channel channel = null; try { channel = connection.createChannel(); channel.exchangeDeclare(baseSource, "topic"); String queueName = cli.getClientID() + "_SUBS_2_" + baseSource + "." + select; channel.queueDeclare(queueName, false, true, false, null); channel.queueBind(queueName, baseSource, select); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); isRunning = true; while (isRunning) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(10); if (delivery != null && isRunning) runnableSubsActor.tell(delivery, null); } catch (InterruptedException e) { e.printStackTrace(); } } if (channel.getConnection().isOpen()) channel.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (channel.getConnection().isOpen()) channel.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public boolean isRunning() { return isRunning; } @Override public void start() { new Thread(this).start(); } @Override public void stop() { isRunning = false; try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } }; consumer.start(); ret = new MomAkkaService().setMsgWorker(subsActor).setConsumer(consumer) .setClient(super.getMomClient()); super.getServices().add(ret); } return ret; }
From source file:net.roboconf.messaging.internal.utils.RabbitMqUtils.java
License:Apache License
/** * Declares the required exchanges for an application. * <p>//from www . j a v a 2s .c om * Every time the DM or an agent must send a message, we must be sure * all the exchanges have been declared. Otherwise, it will result in * an error in the client. And this error will close the channel. * </p> * <p> * To PREVENT stupid errors, it is really important to declare * both exchanges at once! * </p> * * @param applicationName the application name * @param channel the RabbitMQ channel * @throws IOException if an error occurs */ public static void declareApplicationExchanges(String applicationName, Channel channel) throws IOException { // Exchange declaration is idem-potent String dmExchangeName = buildExchangeName(applicationName, true); channel.exchangeDeclare(dmExchangeName, "fanout"); // "fanout" is a keyword for RabbitMQ. // It broadcasts all the messages to all the queues this exchange knows. String agentExchangeName = buildExchangeName(applicationName, false); channel.exchangeDeclare(agentExchangeName, "topic"); // "topic" is a keyword for RabbitMQ. }
From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtils.java
License:Apache License
/** * Declares the required exchanges for an application (only for agents). * @param domain the domain name/*from ww w . java2s .c o m*/ * @param applicationName the application name * @param channel the RabbitMQ channel * @throws IOException if an error occurs */ public static void declareApplicationExchanges(String domain, String applicationName, Channel channel) throws IOException { // "topic" is a keyword for RabbitMQ. if (applicationName != null) { String exch = buildExchangeNameForAgent(domain, applicationName); channel.exchangeDeclare(exch, "topic"); } }
From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtils.java
License:Apache License
/** * Declares the global exchanges (those that do not depend on an application). * <p>//from w w w .jav a 2s .c om * It includes the DM exchange and the one for inter-application exchanges. * </p> * * @param channel the RabbitMQ channel * @throws IOException if an error occurs */ public static void declareGlobalExchanges(String domain, Channel channel) throws IOException { // "topic" is a keyword for RabbitMQ. channel.exchangeDeclare(buildExchangeNameForTheDm(domain), "topic"); channel.exchangeDeclare(buildExchangeNameForInterApp(domain), "topic"); }