List of usage examples for com.rabbitmq.client ConnectionFactory newConnection
public Connection newConnection() throws IOException, TimeoutException
From source file:dfki.sb.rabbitmqjava.RabbitMQServer.java
License:Open Source License
public static void main(String[] argv) { Connection connection = null; Channel channel = null;/* ww w . jav a 2s. c o m*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); System.out.println("Starting server waiting for client requests:"); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); DataInputStream dis = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(delivery.getBody()))); try { int type = dis.readInt(); byte[] response; if (type == 2) { response = handleMarketRequest(dis); } else { response = handleQuoteRequest(dis); } channel.basicPublish("", props.getReplyTo(), replyProps, response); dis.close(); } catch (IOException | ClassNotFoundException e) { System.out.println(" [.] " + e.toString()); } finally { channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (IOException | InterruptedException | ShutdownSignalException | ConsumerCancelledException e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (IOException ignore) { } } } }
From source file:dk.bankjsonrabbit.messaging.Receive.java
public static HashMap<String, Object> setUpReceiver() throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1);//w ww. ja va 2s. c o m QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(TASK_QUEUE_NAME, false, consumer); HashMap<String, Object> returnObjects = new HashMap<>(); returnObjects.put("channel", channel); returnObjects.put("consumer", consumer); return returnObjects; }
From source file:dk.bankjsonrabbit.messaging.Send.java
public static void sendMessage(String message, BasicProperties props) throws IOException, TimeoutException { String taskQueueName = props.getReplyTo(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(taskQueueName, true, false, false, null); channel.basicPublish("", taskQueueName, props, message.getBytes()); channel.close();//from w w w.j av a2s .c om connection.close(); }
From source file:dk.cphbusiness.group11.Translators.PoorBankWS.TranslatorPoorBankWS.java
private void parseAndProcessXmlMessage(String xmlMessage) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document loanRequestXml = builder.parse(new ByteArrayInputStream(xmlMessage.getBytes())); XPath xPath = XPathFactory.newInstance().newXPath(); Element loanDetailsElement = (Element) xPath.compile("/LoanDetails").evaluate(loanRequestXml, XPathConstants.NODE); String ssn = loanDetailsElement.getElementsByTagName("ssn").item(0).getTextContent(); int creditScore = Integer .parseInt(loanDetailsElement.getElementsByTagName("creditScore").item(0).getTextContent()); double loanAmount = Double .parseDouble(loanDetailsElement.getElementsByTagName("loanAmount").item(0).getTextContent()); String temp = loanDetailsElement.getElementsByTagName("loanDurationInMonths").item(0).getTextContent(); int loanDurationInMonths = Integer.parseInt(temp); PoorBankService_Service service = new PoorBankService_Service(); PoorBankService port = service.getPoorBankServiceImplPort(); PoorLoanResponsee result = port.poorLoan(ssn, creditScore, loanAmount, loanDurationInMonths); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(SENDING_QUEUE, "fanout"); String xmlReturnMessage = "<LoanResponse>" + "<interestRate>" + result.getInterestRate() + "</interestRate> \n" + " <ssn>" + result.getSsn() + "</ssn> \n" + "</LoanResponse>"; channel.basicPublish(SENDING_QUEUE, "", null, xmlReturnMessage.getBytes()); }
From source file:dk.cphbusiness.group11.Translators.PoorBankWS.TranslatorPoorBankWS.java
public void run() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(RECEIVING_QUEUE, "fanout"); String queueName = channel.queueDeclare(RECEIVING_QUEUE, false, false, false, null).getQueue(); channel.queueBind(queueName, RECEIVING_QUEUE, ""); System.out.println("Waiting for messages on queue: " + RECEIVING_QUEUE); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (this.isRunning) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("Received '" + message + "'"); this.parseAndProcessXmlMessage(message); }/*from w w w . j a va 2s . c om*/ }
From source file:dk.getcreditscore.messaging.Receive.java
public static HashMap<String, Object> setUpReceiver() throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1);/*from www . j av a 2 s .c o m*/ QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(TASK_QUEUE_NAME, false, consumer); HashMap<String, Object> returnObjects = new HashMap<String, Object>(); returnObjects.put("channel", channel); returnObjects.put("consumer", consumer); return returnObjects; }
From source file:dk.getcreditscore.messaging.Send.java
public static void sendMessage(String message) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); channel.close();/* w ww .j a v a 2 s. c om*/ connection.close(); }
From source file:dk.normalizer.messaging.Receive.java
public static HashMap<String, Object> setUpReceiver() throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1);//from ww w . ja v a 2 s . c om QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(TASK_QUEUE_NAME, false, consumer); HashMap<String, Object> returnObjects = new HashMap<>(); returnObjects.put("channel", channel); returnObjects.put("consumer", consumer); return returnObjects; }
From source file:dreamteamjson.DreamTeamJSON.java
/** * @param args the command line arguments * @throws java.io.IOException// w w w . j a v a2 s . c om */ 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(); Channel listeningChannel = connection.createChannel(); Channel sendingChannel = connection.createChannel(); 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 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 + "'"); Message m = gson.fromJson(message, Message.class); System.out.println(m.toString()); double interestRate = 14; if (m.creditScore > 600) { interestRate -= 4.5; } else if (m.creditScore < 601 && m.creditScore > 500) { interestRate -= 2.7; } else if (m.creditScore < 501 && m.creditScore > 400) { interestRate -= 0.9; } int durationCut = m.loanDuration / 360; interestRate -= (durationCut * 0.18); double amountCut = m.loanAmount / 100000; interestRate -= (amountCut * 0.18); // Insert bank logic String loanResponse = "{\"interestRate\":" + interestRate + ",\"ssn\":" + m.ssn + "}"; sendingChannel.queueDeclare(SENDING_QUEUE_NAME, false, false, false, null); sendingChannel.basicPublish("", SENDING_QUEUE_NAME, null, loanResponse.getBytes()); } }; listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer); }
From source file:dreamteamxmltranslator.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(); Channel channel = connection.createChannel(); channel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null); channel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE_NAME, "DreamTeamBankXML"); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override/*from www . j av a 2 s. c o 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(","); tester(arr); } catch (IOException_Exception ex) { Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex); } } }; channel.basicConsume(LISTENING_QUEUE_NAME, true, consumer); }