List of usage examples for com.rabbitmq.client ConnectionFactory setHost
public void setHost(String host)
From source file:com.wakkir.rabbitmq.basic.Sender.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); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();// w w w . j a v a 2 s .c o m connection.close(); }
From source file:com.workingflow.akka.commons.rabbit.ConnectionFactoryProducer.java
License:Apache License
@Produces @Singleton/* w ww . j a v a2 s. c om*/ public ConnectionFactory getConnectionFactory() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); return factory; }
From source file:com.wss.qvh.log.ConsumerThread.java
public void consumer() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(Constant.RABBITMQ_USERNAME); factory.setPassword(Constant.RABBITMQ_PASSWORD); factory.setVirtualHost(Constant.RABBITMQ_VIRTUAL_HOST); factory.setRequestedChannelMax(Constant.NUM_PRODUCER); factory.setHost(Constant.RABBITMQ_HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(Constant.QUEUE_NAME, Constant.QUEUE_DURABLE, false, false, null); channel.basicQos(Constant.QUEUE_PREFETCH); QueueingConsumer consumer = new QueueingConsumer(channel); boolean autoAck = false; channel.basicConsume(Constant.QUEUE_NAME, autoAck, consumer); while (true) { QueueingConsumer.Delivery delivery; try {//from w w w . j a va 2s .com delivery = consumer.nextDelivery(); } catch (InterruptedException | ShutdownSignalException | ConsumerCancelledException ex) { logger.warn(ex.getMessage(), ex); try { Thread.sleep(100L); } catch (InterruptedException ex1) { logger.warn(ex1.getMessage(), ex1); } continue; } String message; try { message = getMessenge(delivery); } catch (InterruptedException ex) { logger.warn(ex.getMessage(), ex); try { Thread.sleep(100L); } catch (InterruptedException ex1) { logger.warn(ex1.getMessage(), ex1); } continue; } if (message == null || message.isEmpty()) { try { Thread.sleep(100); } catch (InterruptedException ex) { logger.warn(ex.getMessage(), ex); } } else { LogObject lo = new LogObject(); try { lo.parseJson(message); store(lo); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (Exception ex) { logger.debug(message, ex); } } } }
From source file:com.zigbee.function.util.MessageUtil.java
License:Open Source License
public static Channel openChannel(String queueName) { ConnectionFactory factory = new ConnectionFactory(); Channel channel;// w w w . j a va2s. co m try { factory.setHost("45.63.124.106"); Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, false, null); return channel; } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } return null; }
From source file:controllers.TargetController.java
License:Open Source License
/** * This method pushes a message onto a RabbitMQ queue for given target * using global settings from project configuration file. * * @param target The field URL of the target * @return/* w w w . j a va 2 s . c om*/ */ public static Result archive(Long id) { Target target = Target.findById(id); Logger.debug("archiveTarget() " + target); if (target != null) { if (!target.isInScopeAllOrInheritedWithoutLicense()) { return ok(infomessage.render("On-demand archiving is only supported for NPLD targets.")); } // Send the message: try { String queueHost = Play.application().configuration().getString(Const.QUEUE_HOST); String queuePort = Play.application().configuration().getString(Const.QUEUE_PORT); String queueName = Play.application().configuration().getString(Const.QUEUE_NAME); String routingKey = Play.application().configuration().getString(Const.ROUTING_KEY); String exchangeName = Play.application().configuration().getString(Const.EXCHANGE_NAME); Logger.debug("archiveTarget() queue host: " + queueHost); Logger.debug("archiveTarget() queue port: " + queuePort); Logger.debug("archiveTarget() queue name: " + queueName); Logger.debug("archiveTarget() routing key: " + routingKey); Logger.debug("archiveTarget() exchange name: " + exchangeName); JsonNode jsonData = Json.toJson(target); String message = jsonData.toString(); Logger.debug("Crawl Now message: " + message); ConnectionFactory factory = new ConnectionFactory(); if (queueHost != null) { factory.setHost(queueHost); } if (queuePort != null) { factory.setPort(Integer.parseInt(queuePort)); } Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "direct", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); BasicProperties.Builder propsBuilder = new BasicProperties.Builder(); propsBuilder.deliveryMode(2); channel.basicPublish(exchangeName, routingKey, propsBuilder.build(), message.getBytes()); channel.close(); connection.close(); } catch (IOException e) { String msg = "There was a problem queuing this crawl instruction. Please refer to the system administrator."; User currentUser = User.findByEmail(request().username()); StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String msgFullTrace = sw.toString(); Logger.error(msgFullTrace); if (currentUser.hasRole("sys_admin")) { msg = msgFullTrace; } return ok(infomessage.render(msg)); } catch (Exception e) { String msg = "There was a problem queuing this crawl instruction. Please refer to the system administrator."; User currentUser = User.findByEmail(request().username()); StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String msgFullTrace = sw.toString(); Logger.error(msgFullTrace); if (currentUser.hasRole("sys_admin")) { msg = msgFullTrace; } return ok(infomessage.render(msg)); } } else { Logger.debug("There was a problem sending the message. Target field for archiving is empty"); return ok(infomessage .render("There was a problem sending the message. Target field for archiving is empty")); } return ok(ukwalicenceresult.render()); }
From source file:cs.rsa.ts14dist.appserver.RabbitMQDaemon.java
License:Apache License
@Override public void run() { Connection connection = null; Channel channel = null;//from ww w. ja v a2 s.c om try { ConnectionFactory factory = new ConnectionFactory(); logger.info("Starting RabbitMQDaemon, MQ IP = " + hostname); factory.setHost(hostname); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(rpcQueueName, false, false, false, null); channel.basicQos(1); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(rpcQueueName, false, consumer); while (true) { String response = null; // Block and fetch next msg from queue QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); try { String message = new String(delivery.getBody(), "UTF-8"); logger.trace("Received msg: " + message); // Convert the message to a JSON request object JSONObject request = (JSONObject) JSONValue.parse(message); // Delegate to the server request handler for handling the // request and computing an answer JSONObject reply = serverRequestHandler.handleRequest(request); // Convert the answer back into a string for replying to // client response = reply.toJSONString(); } catch (Exception e) { logger.error(" Exception in MQDAemon run()/while true] " + e.toString()); response = "wrong"; } finally { logger.trace("Returning answer: " + response); channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8")); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); logger.trace("Answer acknowledged."); } } } catch (Exception e) { logger.error("Exception in daemon's outer loop: nested exception" + e.getMessage()); e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:cs.rsa.ts14dist.appserver.RabbitMQRPCConnector.java
License:Apache License
/** Create the connector using the indicated RabbitMQ * instance(s) as messaging queue./* w w w . jav a2s . co m*/ * * @param hostname * @throws IOException */ public RabbitMQRPCConnector(String hostname) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); connection = factory.newConnection(); channelHighPriority = connection.createChannel(); channelLowPriority = connection.createChannel(); replyHighPriorityQueueName = channelHighPriority.queueDeclare().getQueue(); consumerHighPriority = new QueueingConsumer(channelHighPriority); channelHighPriority.basicConsume(replyHighPriorityQueueName, true, consumerHighPriority); replyLowPriorityQueueName = channelLowPriority.queueDeclare().getQueue(); consumerLowPriority = new QueueingConsumer(channelLowPriority); channelLowPriority.basicConsume(replyLowPriorityQueueName, true, consumerLowPriority); logger.info("Opening connector on MQ at IP= " + hostname); }
From source file:cu.uci.plagicoj.config.DIConfiguration.java
@Bean public ConnectionFactory connectionFactory() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(env.getProperty("amqp.server")); factory.setPassword("cojrabbit123*-+"); return factory; }
From source file:de.dhbw.mannheim.assemblylinesim.AssemblyLineSim.java
License:Open Source License
/** * * @param hostname of the rabbitMq/* ww w . j a v a 2 s . c om*/ * @param speedUpFactor speed up factor to speed up simulation, e.g. 10 --> 10 times faster * @throws IOException is thrown if the connection to rabbitmq fails */ private AssemblyLineSim(String hostname, double speedUpFactor) throws IOException { (new RabbitListener(hostname)).start(); this.speedUpFactor = speedUpFactor; ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(REPORT_EXCHANGE_NAME, "fanout"); }
From source file:de.dhbw.mannheim.erpsim.ErpSimulator.java
License:Open Source License
/** * * * @param args command line parameter/*w w w .j av a 2 s. c om*/ * args[0] number of customer orders that should be created * args[1] hostname of rabbitMQ * @throws IOException */ public static void main(String[] args) throws IOException { int numOfCustomerOrder = 10; String rabbitMqHostName = "localhost"; String rabbitMqUserName = null; String rabbitMqPassword = null; if (args.length >= 1) { try { numOfCustomerOrder = Integer.parseInt(args[0]); System.out.println("Number of customer orders: " + numOfCustomerOrder); } catch (Exception e) { System.err.println("Could not parse number of customer orders " + args[0]); } } if (args.length >= 2 && args[1] != null) { rabbitMqHostName = args[1]; System.out.println("Host of rabbitMq: " + rabbitMqHostName); } if (args.length >= 4 && args[2] != null && args[3] != null) { rabbitMqUserName = args[2]; rabbitMqPassword = args[3]; System.out.println("Username of rabbitMq: " + rabbitMqUserName); } CustomerOrder[] customerOrders = new CustomerOrder[numOfCustomerOrder]; for (int i = 0; i < customerOrders.length; i++) { customerOrders[i] = CustomerOrderGenerator.getCustomOrder(); } XStream xstream = new XStream(); xstream.registerConverter(new Converter() { @Override public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) { MachineOrder mo = (MachineOrder) o; writer.startNode("id"); writer.setValue(mo.getId()); writer.endNode(); } @Override public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) { return null; } @Override public boolean canConvert(Class aClass) { return aClass == MachineOrder.class; } }); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMqHostName); if (rabbitMqPassword != null && rabbitMqUserName != null) { factory.setUsername(rabbitMqUserName); factory.setPassword(rabbitMqPassword); } Connection connection = factory.newConnection(); Channel channelCO = connection.createChannel(); channelCO.exchangeDeclare(CUSTOMER_ORDER_EXCHANGE_NAME, "fanout"); for (CustomerOrder co : customerOrders) { String message = xstream.toXML(co); channelCO.basicPublish(CUSTOMER_ORDER_EXCHANGE_NAME, "", null, message.getBytes()); System.out.println("Send customer order"); } channelCO.close(); Channel channelMO = connection.createChannel(); channelMO.exchangeDeclare(MACHINE_ORDER_EXCHANGE_NAME, "fanout"); MachineOrder mo = MachineOrderGenerator.getRandomMachineOrder(); xstream = new XStream(); // reconstruct XStream to parse the full machine order, not just only the id while (mo != null) { int i = System.in.read(); String message = xstream.toXML(mo); channelMO.basicPublish(MACHINE_ORDER_EXCHANGE_NAME, "", null, message.getBytes()); System.out.println("Send Machine order"); mo = MachineOrderGenerator.getRandomMachineOrder(); } channelMO.close(); connection.close(); }