List of usage examples for com.rabbitmq.client ConnectionFactory setHost
public void setHost(String host)
From source file:org.opendaylight.federationmessagequeue.impl.RabbitMessageBus.java
License:Open Source License
@Override public boolean createQueue(String queueName, String mqBrokerIp, int mqPortNumber, String mqUser, String mqUserPwd) {//from w w w . j a v a2 s .co m LOG.info("Creating connection for queue {} on broker {}", queueName, mqBrokerIp); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(mqBrokerIp); factory.setPort(mqPortNumber); factory.setUsername(mqUser); factory.setPassword(mqUserPwd); factory.setAutomaticRecoveryEnabled(true); try { Connection connection = factory.newConnection(); LOG.info("Created connection to broker {}:{} for user {} ", mqBrokerIp, mqPortNumber, mqUser); Channel channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, false, null); LOG.info("Declared queue {} on broker {}", queueName, mqBrokerIp); MessageBusConnectionData mbcd = new MessageBusConnectionData(mqBrokerIp, connection, channel); queueNameToConnectionData.put(queueName, mbcd); return true; } catch (IOException | TimeoutException e) { LOG.warn("Failed creating queue {} on broker {}:{} for user {} because: {}", queueName, mqBrokerIp, mqPortNumber, mqUser, e.getMessage()); return false; } }
From source file:org.openmrs.module.amqpmodule.utils.impl.PublisherServiceImpl.java
License:Open Source License
@Override public boolean PublisherCreateConnection() throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.43.123"); factory.setPort(5672);//from w w w .j a v a2 s. co m factory.setUsername("chs"); factory.setPassword("chs123"); Connection connection = null; try { connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct", true); channel.basicPublish(EXCHANGE_NAME, topic, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes()); System.out.println(" [x] Sent '" + msg + "'"); channel.close(); } catch (TimeoutException e) { System.out.println("Connection Timed out"); e.printStackTrace(); } connection.close(); return true; }
From source file:org.opennaas.extensions.genericnetwork.capability.nclprovisioner.components.NetworkObservationsPusher.java
License:Apache License
private void sendStatistics(String topicName, String csvMessage) throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException { Connection conn = null;/* w ww.j a v a 2 s . c om*/ Channel channel = null; try { log.debug("Establishing RabbitMQ connection with sla manager with URI: " + slaManagerUri); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(slaManagerUri.getHost()); conn = factory.newConnection(); channel = conn.createChannel(); channel.exchangeDeclare(topicName, "direct"); log.debug("Publishing message in RabbitMQ channel."); channel.basicPublish(topicName, RABBIT_MQ_ROUTING_KEY, new AMQP.BasicProperties.Builder().contentType(OBSERVATIONS_CONTENT_TYPE).build(), csvMessage.getBytes()); log.debug("Message successfully sent to SLA manager."); } finally { if (channel != null && channel.isOpen()) channel.close(); if (conn != null && conn.isOpen()) conn.close(); log.debug("Connection to RabbitMQ server closed."); } }
From source file:org.pushtrigger.mule.agent.CreateQueueAgent.java
License:Apache License
@Override public void start() throws MuleException { System.out.println("Create Queue Agent is running..."); Connection connection = null; Channel channel = null;/*from w w w.j a va 2s. co m*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World! I am creating a queue..."; Map<String, Object> props = new HashMap<String, Object>(); props.put("path", "queue"); props.put("branch", "create"); AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder(); AMQP.BasicProperties basicProps = bob.headers(props).build(); channel.basicPublish("", QUEUE_NAME, basicProps, message.getBytes()); System.out.println("Agent has created the queue..."); } catch (IOException e) { System.out.println("Something wrong " + e); } finally { try { if (channel != null) channel.close(); } catch (IOException e) { } try { if (connection != null) connection.close(); } catch (IOException e) { } } }
From source file:org.s23m.cell.repository.client.connector.RepositoryClientConnector.java
License:Mozilla Public License
private void initClient() { final ConnectionFactory cfconn = new ConnectionFactory(); cfconn.setHost(ConfigValues.getString("RepositoryClientServer.HOST_NAME")); cfconn.setPort(Integer.parseInt(ConfigValues.getString("RepositoryClientServer.PORT"))); cfconn.setVirtualHost(ConfigValues.getString("RepositoryClientServer.VHOST_NAME")); cfconn.setUsername(ConfigValues.getString("RepositoryClientServer.USER_NAME")); cfconn.setPassword(ConfigValues.getString("RepositoryClientServer.PW")); Connection conn;//from w ww . ja v a 2 s .com try { conn = cfconn.newConnection(); final Channel ch = conn.createChannel(); clientService = new RpcClient(ch, "", ConfigValues.getString("RepositoryClientServer.QUEUE")); } catch (final IOException ex) { throw new IllegalStateException("Client set up failed", ex); } }
From source file:org.s23m.cell.repository.client.server.RepositoryClientServer.java
License:Mozilla Public License
private void setupConnection() throws IOException { final ConnectionFactory connFactory = new ConnectionFactory(); connFactory.setHost(LOCALHOST); connFactory.setPort(PORT);//from w ww . j a va 2 s . c om connFactory.setVirtualHost(ConfigValues.getString("RepositoryClientServer.VHOST_NAME")); connFactory.setUsername(ConfigValues.getString("RepositoryClientServer.USER_NAME")); connFactory.setPassword(ConfigValues.getString("RepositoryClientServer.PW")); final Connection conn = connFactory.newConnection(); ch = conn.createChannel(); ch.queueDeclare(QUEUE_NAME, false, false, false, null); }
From source file:org.seaduck.murrelet.impl.rabbitmq.AsyncReceiver.java
License:Apache License
public AsyncReceiver(String busName, String host) { super(busName); this.logger = LoggerFactory.getLogger(AsyncReceiver.class); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); try {//from ww w .j a v a 2 s. c o m this.connection = factory.newConnection(); this.channel = connection.createChannel(); channel.queueDeclare(busName, false, false, false, null); } catch (IOException e) { this.logger.error("Error in opening connection."); e.printStackTrace(); } this.logger.info("Bus established: " + super.getBusName()); }
From source file:org.seaduck.murrelet.impl.rabbitmq.AsyncSender.java
License:Apache License
public AsyncSender(String busName, String host) { super(busName); this.logger = LoggerFactory.getLogger(AsyncSender.class); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); try {/*from w w w . jav a 2 s . c om*/ this.connection = factory.newConnection(); this.channel = connection.createChannel(); } catch (IOException e) { this.logger.error("Error in opening connection."); e.printStackTrace(); } this.logger.info("Bus established: " + super.getBusName()); }
From source file:org.smartdeveloperhub.curator.connector.BrokerController.java
License:Apache License
void connect() throws ControllerException { this.write.lock(); try {// ww w .j a v a 2 s . c om if (this.connected) { return; } final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.broker.host()); factory.setPort(this.broker.port()); factory.setVirtualHost(this.broker.virtualHost()); factory.setThreadFactory(brokerThreadFactory()); factory.setExceptionHandler(new BrokerControllerExceptionHandler(this)); this.connection = factory.newConnection(); createChannel(); } catch (IOException | TimeoutException e) { this.connected = false; final String message = String.format("Could not connect to broker at %s:%s using virtual host %s", this.broker.host(), this.broker.port(), this.broker.virtualHost()); throw new ControllerException(message, e); } finally { this.write.unlock(); } }
From source file:org.smartdeveloperhub.harvesters.it.notification.ConnectionManager.java
License:Apache License
void connect() throws ControllerException { this.lock.lock(); try {//w w w .j a v a 2 s . c o m if (connected()) { return; } final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.brokerHost); factory.setPort(this.brokerPort); factory.setVirtualHost(this.virtualHost); factory.setThreadFactory(brokerThreadFactory()); factory.setExceptionHandler(new ConnectionManagerExceptionHandler(this)); this.connection = factory.newConnection(); createChannel(); } catch (IOException | TimeoutException e) { final String message = String.format("Could not connect to broker at %s:%s using virtual host %s", this.brokerHost, this.brokerPort, this.virtualHost); throw new ControllerException(this.brokerHost, this.brokerPort, this.virtualHost, message, e); } finally { this.lock.unlock(); } }