List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:com.magikbyte.smalldoode.meeting.Manager.java
public void init() { try {// w ww .j a v a 2 s . c om ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); 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"); dispatchMessage(message); } }; channel.basicConsume(QUEUE_NAME, true, consumer); } catch (IOException | TimeoutException e) { } }
From source file:com.magikbyte.smalldoode.meeting.model.Groupe.java
public void init() { try {//w w w. j a va 2s. c o m ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(this.name, "topic"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, this.name, this.name); 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[] msg; String message = new String(body, "UTF-8"); if (message.startsWith("msg,")) { messages.add(message); } else if (message.startsWith("dateAdmin")) { if (dateProposeParAdmin.size() < maxChoixPossiblePourUneReunion) { message = message.replace("dateAdmin,", ""); dateProposeParAdmin.add(message.split(",")[1]); } } else if (message.startsWith("dateUser")) { message = message.replace("dateUser,", ""); msg = message.split(","); List<String> mesMessages = dateProposeParLesAgents.get(msg[0].trim()); if (mesMessages == null) { mesMessages = new ArrayList<>(); } mesMessages.add(msg[1]); dateProposeParLesAgents.put(msg[0].trim(), mesMessages); } setChanged(); notifyObservers(); System.out.println(" [x] Received " + message); } }; channel.basicConsume(queueName, true, consumer); } catch (IOException | TimeoutException e) { e.printStackTrace(); } }
From source file:com.Main.Aggregator.java
public static void main(String[] argv) throws IOException { Channel channel = rabbitMQUtil.createQueue(QUEUE_NAME_RECEIVE); Consumer consumer = new DefaultConsumer(channel) { @Override//from www . j a v a 2 s. com public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { ReplyObject replyObject = (ReplyObject) StringByteHelper.fromByteArrayToObject(body); if (waitingList.isEmpty()) { waitingList.add(replyObject); System.out.println("Object added to Array"); initialise = time.getTimeInMillis(); } else { if (replyObject.getSsn().equals(waitingList.get(0).getSsn())) { waitingList.add(replyObject); } } if (!waitingList.isEmpty()) { ReplyObject finalReply = waitingList.get(0); if (initialise + 3000 <= time.getTimeInMillis()) { System.out.println("" + time.getTime()); for (ReplyObject reply : waitingList) { int result = reply.getInterestRate().compareTo(finalReply.getInterestRate()); if (result > 1) { finalReply = reply; } } try { sendFinalReply(finalReply); waitingList.clear(); } catch (IOException e) { e.printStackTrace(); } } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }; channel.basicConsume(QUEUE_NAME_RECEIVE, true, consumer); }
From source file:com.mycompany.aggregator.Aggregator.java
public void subscribe() throws Exception, TimeoutException, IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setUsername(USERNAME);/*from w w w . j av a 2 s . c o m*/ factory.setPassword(PASSWORD); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); 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 response = new String(body, "UTF-8"); System.out.println("Received message!!"); synchronized (responseQueueBuffer) { if (responseQueueBuffer.get(properties.getCorrelationId()) == null) { responseQueueBuffer.put(properties.getCorrelationId(), new ResponseBuffer()); } if (response != null) { responseQueueBuffer.get(properties.getCorrelationId()) .addResponse(new JSONObject(response)); } } } }; channel.basicConsume(QUEUE_NAME, true, consumer); Thread task = new Thread() { @Override public void run() { while (true) { syncQueues(); for (Map.Entry<String, ResponseBuffer> pair : responseQueue.entrySet()) { if (responseQueue.get(pair.getKey()).getCreatedAt() + TIMEOUT < System .currentTimeMillis()) { JSONObject bestResponse = responseQueue.get(pair.getKey()).getBestResponse(); try { forwardToSockets(bestResponse, (String) pair.getKey()); System.out.println("Should return " + bestResponse); responseQueue.get(pair.getKey()).setFinished(); } catch (IOException ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } } } } } }; task.start(); }
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 w w . j av a 2s . c o 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 ww . j av a 2 s.c om 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 v a 2s . c o m*/ 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 ww . j a v a 2s.co m 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.Recv.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.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override//from www . j a va 2 s. c o m 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(QUEUE_NAME, 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);//from ww w . j a v a 2s.c o m } 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); }