List of usage examples for com.rabbitmq.client ConnectionFactory setHost
public void setHost(String host)
From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtils.java
License:Apache License
/** * Configures the connection factory with the right settings. * @param factory the connection factory * @param configuration the messaging configuration * @throws IOException if something went wrong * @see RabbitMqConstants//from w w w. j a v a2s . c o m */ public static void configureFactory(ConnectionFactory factory, Map<String, String> configuration) throws IOException { final Logger logger = Logger.getLogger(RabbitMqUtils.class.getName()); logger.fine("Configuring a connection factory for RabbitMQ."); String messageServerIp = configuration.get(RABBITMQ_SERVER_IP); if (messageServerIp != null) { Map.Entry<String, Integer> entry = Utils.findUrlAndPort(messageServerIp); factory.setHost(entry.getKey()); if (entry.getValue() > 0) factory.setPort(entry.getValue()); } factory.setUsername(configuration.get(RABBITMQ_SERVER_USERNAME)); factory.setPassword(configuration.get(RABBITMQ_SERVER_PASSWORD)); // Timeout for connection establishment: 5s factory.setConnectionTimeout(5000); // Configure automatic reconnection factory.setAutomaticRecoveryEnabled(true); // Recovery interval: 10s factory.setNetworkRecoveryInterval(10000); // Exchanges and so on should be redeclared if necessary factory.setTopologyRecoveryEnabled(true); // SSL if (Boolean.parseBoolean(configuration.get(RABBITMQ_USE_SSL))) { logger.fine("Connection factory for RabbitMQ: SSL is used."); InputStream clientIS = null; InputStream storeIS = null; try { clientIS = new FileInputStream(configuration.get(RABBITMQ_SSL_KEY_STORE_PATH)); storeIS = new FileInputStream(configuration.get(RABBITMQ_SSL_TRUST_STORE_PATH)); char[] keyStorePassphrase = configuration.get(RABBITMQ_SSL_KEY_STORE_PASSPHRASE).toCharArray(); KeyStore ks = KeyStore.getInstance( getValue(configuration, RABBITMQ_SSL_KEY_STORE_TYPE, DEFAULT_SSL_KEY_STORE_TYPE)); ks.load(clientIS, keyStorePassphrase); String value = getValue(configuration, RABBITMQ_SSL_KEY_MNGR_FACTORY, DEFAULT_SSL_MNGR_FACTORY); KeyManagerFactory kmf = KeyManagerFactory.getInstance(value); kmf.init(ks, keyStorePassphrase); char[] trustStorePassphrase = configuration.get(RABBITMQ_SSL_TRUST_STORE_PASSPHRASE).toCharArray(); KeyStore tks = KeyStore.getInstance( getValue(configuration, RABBITMQ_SSL_TRUST_STORE_TYPE, DEFAULT_SSL_TRUST_STORE_TYPE)); tks.load(storeIS, trustStorePassphrase); value = getValue(configuration, RABBITMQ_SSL_TRUST_MNGR_FACTORY, DEFAULT_SSL_MNGR_FACTORY); TrustManagerFactory tmf = TrustManagerFactory.getInstance(value); tmf.init(tks); SSLContext c = SSLContext .getInstance(getValue(configuration, RABBITMQ_SSL_PROTOCOL, DEFAULT_SSL_PROTOCOL)); c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); factory.useSslProtocol(c); } catch (GeneralSecurityException e) { throw new IOException("SSL configuration for the RabbitMQ factory failed.", e); } finally { Utils.closeQuietly(storeIS); Utils.closeQuietly(clientIS); } } }
From source file:net.yacy.grid.io.messages.RabbitQueueFactory.java
License:Open Source License
private void init() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.server); if (this.port > 0) factory.setPort(this.port); if (this.username != null && this.username.length() > 0) factory.setUsername(this.username); if (this.password != null && this.password.length() > 0) factory.setPassword(this.password); try {/*from w w w . j a v a 2 s . c o m*/ this.connection = factory.newConnection(); this.channel = connection.createChannel(); this.queues = new ConcurrentHashMap<>(); } catch (TimeoutException e) { throw new IOException(e.getMessage()); } }
From source file:nl.uva.sne.drip.api.rpc.DRIPCaller.java
License:Apache License
public DRIPCaller(String messageBrokerHost, String requestQeueName) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(messageBrokerHost); factory.setPort(AMQP.PROTOCOL.PORT); //factory.setUsername("guest"); //factory.setPassword("pass"); connection = factory.newConnection(); channel = connection.createChannel(); // create a single callback queue per client not per requests. replyQueueName = channel.queueDeclare().getQueue(); this.requestQeueName = requestQeueName; this.mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); }
From source file:nl.uva.sne.drip.drip.component_example.RPCServer.java
License:Apache License
private static void start() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST); factory.setPassword("guest"); factory.setUsername("guest"); factory.setPort(AMQP.PROTOCOL.PORT); try (Connection connection = factory.newConnection()) { Channel channel = connection.createChannel(); //We define the queue name channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); //Set our own customized consummer Consumer c = new Consumer(channel); //Start listening for messages channel.basicConsume(RPC_QUEUE_NAME, false, c); //Block so we don't close the channel while (true) { try { Thread.sleep(100); } catch (InterruptedException _ignore) { }/*from w w w . j a va2 s .c o m*/ } } catch (IOException | TimeoutException ex) { Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nl.uva.sne.drip.drip.provisioner.RPCServer.java
License:Apache License
private static void start() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(PropertyValues.HOST); factory.setPassword("guest"); factory.setUsername("guest"); factory.setPort(AMQP.PROTOCOL.PORT); Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, "Connected to: {0}", PropertyValues.HOST); try (Connection connection = factory.newConnection()) { Channel channel = connection.createChannel(); //We define the queue name channel.queueDeclare(PropertyValues.RPC_QUEUE_NAME, false, false, false, null); DefaultConsumer c;/*from ww w . java 2 s . c o m*/ if (PropertyValues.RPC_QUEUE_NAME.endsWith("v0")) { c = new nl.uva.sne.drip.drip.provisioner.v0.Consumer(channel); } else { c = new nl.uva.sne.drip.drip.provisioner.v1.Consumer(channel, PropertyValues.HOST); } //Start listening for messages channel.basicConsume(PropertyValues.RPC_QUEUE_NAME, false, c); //Block so we don't close the channel while (true) { try { Thread.sleep(100); } catch (InterruptedException _ignore) { } } } catch (IOException | TimeoutException ex) { Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:OnlyPackege.TheMightyBank.java
public static void main(String[] args) throws Exception { JSONParser jsonParster = new JSONParser(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel recvChannel = connection.createChannel(); recvChannel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String queueName = recvChannel.queueDeclare().getQueue(); recvChannel.queueBind(queueName, EXCHANGE_NAME, ""); QueueingConsumer consumer = new QueueingConsumer(recvChannel); recvChannel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String responseQueue = delivery.getProperties().getReplyTo(); Channel sendChannel = connection.createChannel(); sendChannel.queueDeclare(responseQueue, false, false, false, null); String message = new String(delivery.getBody()); JSONObject recvObj = (JSONObject) jsonParster.parse(message); //to be deleted after testing System.out.println(" [x] Received '" + message + "'"); JSONObject obj = new JSONObject(); obj.put("ssn", recvObj.get("ssn")); obj.put("interestRate", Math.random() * 10); sendChannel.basicPublish("", responseQueue, null, obj.toJSONString().getBytes()); }/*from w w w . j ava2 s.co m*/ }
From source file:org.apache.airavata.datacat.agent.messageBroker.AiravataUpdateListener.java
License:Apache License
public void startBroker() { (new Thread(new Runnable() { @Override/*w ww .ja v a 2 s. c om*/ public void run() { try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RABBITMQ_HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); channel.basicQos(1); channel.queueBind(queueName, EXCHANGE_NAME, BINDING_KEY); logger.debug("Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (runFileUpdateListener) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); Message message = new Message(); ThriftUtils.createThriftFromBytes(delivery.getBody(), message); TBase event = null; if (message.getMessageType().equals(MessageType.EXPERIMENT_OUTPUT)) { ExperimentOutputCreatedEvent experimentOutputCreatedEvent = new ExperimentOutputCreatedEvent(); ThriftUtils.createThriftFromBytes(message.getEvent(), experimentOutputCreatedEvent); logger.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "' with experiment name " + experimentOutputCreatedEvent.getExperimentName()); event = experimentOutputCreatedEvent; logger.debug(" [x] Received FileInfo Message'"); process(experimentOutputCreatedEvent, message.getUpdatedTime()); logger.debug(" [x] Done Processing FileInfo Message"); } else { logger.debug("Recieved message of type ..." + message.getMessageType()); } } } catch (Exception e) { logger.error(e); } } })).start(); }
From source file:org.apache.airavata.datacat.agent.messageBroker.RabbitMQProducer.java
License:Apache License
private Connection createConnection() throws IOException { try {// w ww.jav a 2 s . c om ConnectionFactory factory = new ConnectionFactory(); factory.setHost(url); Connection connection = factory.newConnection(); connection.addShutdownListener(new ShutdownListener() { public void shutdownCompleted(ShutdownSignalException cause) { } }); log.info("connected to rabbitmq: " + connection + " for " + exchangeName); return connection; } catch (Exception e) { log.info("connection failed to rabbitmq: " + connection + " for " + exchangeName); return null; } }
From source file:org.apache.airavata.datacat.agent.org.apache.airavata.datacat.agent.messageBroker.RabbitMQConsumerTest.java
License:Apache License
/** * Test method to publish dummy data to RabbitMQ * @param messageType// ww w .j a v a 2 s . c o m * @throws java.io.IOException * @throws TException */ public void publish(MessageType messageType) throws java.io.IOException, TException { //establishing the connection ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RABBITMQ_HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String DATA_ROOT = AgentProperties.getInstance().getProperty(Constants.DATA_ROOT, ""); //creating the output created Event ExperimentOutputCreatedEvent event = new ExperimentOutputCreatedEvent(); event.setExperimentId("test"); event.setOutputPath(DATA_ROOT + "/2H2OOHNCmin.com.out"); //serializing the event byte[] body = ThriftUtils.serializeThriftObject(event); Message message = new Message(); message.setEvent(body); message.setMessageId("sad"); message.setMessageType(messageType); message.setUpdatedTime(993344232); String routingKey = "*"; //serializing the message object byte[] messageArray = ThriftUtils.serializeThriftObject(message); channel.basicPublish(EXCHANGE_NAME, BINDING_KEY, null, messageArray); logger.debug(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
From source file:org.apache.airavata.gfac.monitor.util.AMQPConnectionUtil.java
License:Apache License
public static Connection connect(String host, String vhost, String proxyFile) { Connection connection;//from w w w. ja va 2 s.c o m try { String keyPassPhrase = "test123"; KeyStore ks = X509Helper.keyStoreFromPEM(proxyFile, keyPassPhrase); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, keyPassPhrase.toCharArray()); KeyStore tks = X509Helper.trustKeyStoreFromCertDir(); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(tks); SSLContext c = SSLContext.getInstance("SSLv3"); c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); factory.setPort(5671); factory.useSslProtocol(c); factory.setVirtualHost(vhost); factory.setSaslConfig(DefaultSaslConfig.EXTERNAL); connection = factory.newConnection(); } catch (Exception e) { e.printStackTrace(); return null; } return connection; }