List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
From source file:localdomain.localhost.RabbitMQClient.java
License:Apache License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//from w w w.j a va2 s .c o m ConnectionFactory factory = new ConnectionFactory(); String messages = new String(); String uri = System.getProperty("CLOUDAMQP_URL"); factory.setUri(uri); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, true, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); boolean autoACK = false; channel.basicConsume(QUEUE_NAME, autoACK, consumer); System.out.println(" [*] Waiting 100ms for a message"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(100); while (delivery != null) { String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); messages = message + " <br/> " + messages; delivery = consumer.nextDelivery(100); } request.setAttribute("messages", messages); request.getRequestDispatcher("/index.jsp").forward(request, response); channel.close(); connection.close(); } catch (Exception e) { request.setAttribute("throwable", e); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
From source file:localdomain.localhost.RabbitMQServer.java
License:Apache License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//from w ww . ja v a 2 s. com String message = request.getParameter("message"); ConnectionFactory factory = new ConnectionFactory(); String uri = System.getProperty("CLOUDAMQP_URL"); factory.setUri(uri); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); boolean durable = true; channel.queueDeclare(QUEUE_NAME, durable, false, false, null); channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); response.sendRedirect(request.getContextPath() + "/index.jsp"); } catch (Exception e) { request.setAttribute("throwable", e); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
From source file:main.TestMain.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(QUEUE_NAME, false, false, false, null); channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();//from w w w . jav a 2 s .c om connection.close(); }
From source file:mapas.Mapas.java
public static void main(String[] args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setPassword("test"); factory.setUsername("test"); final Connection connection = factory.newConnection(); final Channel 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 w w w .j av a 2 s .com*/ final 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 '" + message + "'"); try { doWork(message); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(" [x] Done"); channel.basicAck(envelope.getDeliveryTag(), false); } }; channel.basicConsume(TASK_QUEUE_NAME, false, consumer); }
From source file:messaging.Worker.java
License:Apache License
public static void sendMessage(String message) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename()); 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()); System.out.println(" [x] Sent '" + message + "'"); channel.close();//from w w w.j a v a 2s. com connection.close(); }
From source file:mx.bigdata.utils.amqp.AMQPClientHelperImpl.java
License:Apache License
public Channel declareChannel(ConnectionFactory factory, String key) throws Exception { Connection conn = factory.newConnection(); Channel channel = conn.createChannel(); Integer basicQos = conf.getInteger("channel_basic_qos"); if (basicQos != null) { channel.basicQos(basicQos);// www. j a va 2 s .c om } else { channel.basicQos(DEFAULT_QOS); } channel.exchangeDeclare(getExchangeName(key), getExchangeType(key), true); return channel; }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * Create a message group request service. * * @param source the source where request are coming from * @param requestWorker the application request worker * @return the new message group request service *//* w w w . j av a 2 s. c o m*/ @Override public MomAkkaService msgGroupRequestService(String source, AppMsgWorker requestWorker) { final Connection connection = ((Client) super.getMomClient()).getConnection(); MomAkkaService ret = null; ActorRef requestActor; MomConsumer consumer; MomMsgGroupServiceMgr msgGroupMgr; if (connection != null && connection.isOpen()) { try { Channel channel = connection.createChannel(); channel.basicQos(1); requestActor = ServiceFactory.createRequestRouter(source, super.getMomClient(), channel, requestWorker, null, true); consumer = ServiceFactory.createConsumer(source, channel, requestActor, super.getMomClient().isMsgDebugOnTimeout()); msgGroupMgr = ServiceFactory.createMsgGroupServiceManager(source, channel, requestWorker, super.getMomClient()); consumer.start(); ret = new MomAkkaService().setMsgWorker(requestActor).setConsumer(consumer) .setClient(super.getMomClient()).setMsgGroupServiceMgr(msgGroupMgr); super.getServices().add(ret); } catch (IOException e) { e.printStackTrace(); } } return ret; }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * Create a request service (can not handle message grouping) * * @param source the source where request are coming from * @param requestWorker the application request worker * @return the new request service// ww w .j ava 2s . c o m */ @Override public MomAkkaService requestService(final String source, final AppMsgWorker requestWorker) { final Connection connection = ((Client) super.getMomClient()).getConnection(); MomAkkaService ret = null; ActorRef requestActor; MomConsumer consumer; if (connection != null && connection.isOpen()) { try { Channel channel = connection.createChannel(); channel.basicQos(1); requestActor = ServiceFactory.createRequestRouter(source, super.getMomClient(), channel, requestWorker, null, false); consumer = ServiceFactory.createConsumer(source, channel, requestActor, super.getMomClient().isMsgDebugOnTimeout()); consumer.start(); ret = new MomAkkaService().setMsgWorker(requestActor).setConsumer(consumer) .setClient(super.getMomClient()); super.getServices().add(ret); } catch (IOException e) { e.printStackTrace(); } } return ret; }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * Create a new subscriber service.// ww w .j a v a 2 s. co m * * @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.es.netshell.rabbitmq.Consume.java
License:Open Source License
public void consumeMessage() throws Exception { if (queueName == null) { queueName = new UUIDManager(QUEUE_FILE).checkUUID(); }/* w w w .j a v a 2s.c o m*/ ConnectionFactory factory = new SSLConnection(info).createConnection(); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, true, null); System.out.println(" [*] Waiting for messages."); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, "consumer", false, false, null, consumer); //while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); ByteArrayInputStream bais = new ByteArrayInputStream(delivery.getBody()); ObjectInputStream in = new ObjectInputStream(bais); DefaultListenableGraph g = (DefaultListenableGraph) in.readObject(); System.out.println(" [x] Received Message"); // GraphViewer view = new GraphViewer(g); // view.init(); // if (message.substring(0,13).equals("TOKEN_REQUEST")) { // String[] token = new ProcessTokenRequest(message, channel).sendToken(); // permissions.put(token[0], token[1]); // //String[] messageSplit = message.split(":"); // //sendToken(messageSplit[1], messageSplit[2], channel); // } else { // String[] messageSplit = message.split(":", 2); // if (permissions.containsKey(messageSplit[0])) { // System.out.println(" [x] Received '" + messageSplit[1] + "' from: " + permissions.get(messageSplit[0])); // } else { // System.out.println(" ERROR: INVALID TOKEN PROVIDED IN MESSAGE"); // } // } //} channel.queueDelete(queueName); channel.close(); connection.close(); }