List of usage examples for com.rabbitmq.client ConnectionFactory setVirtualHost
public void setVirtualHost(String virtualHost)
From source file:org.thingsboard.rule.engine.rabbitmq.TbRabbitMqNode.java
License:Apache License
@Override public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { this.config = TbNodeUtils.convert(configuration, TbRabbitMqNodeConfiguration.class); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.config.getHost()); factory.setPort(this.config.getPort()); factory.setVirtualHost(this.config.getVirtualHost()); factory.setUsername(this.config.getUsername()); factory.setPassword(this.config.getPassword()); factory.setAutomaticRecoveryEnabled(this.config.isAutomaticRecoveryEnabled()); factory.setConnectionTimeout(this.config.getConnectionTimeout()); factory.setHandshakeTimeout(this.config.getHandshakeTimeout()); this.config.getClientProperties().forEach((k, v) -> factory.getClientProperties().put(k, v)); try {/*w ww .jav a2 s. co m*/ this.connection = factory.newConnection(); this.channel = this.connection.createChannel(); } catch (Exception e) { throw new TbNodeException(e); } }
From source file:org.trianacode.TrianaCloud.Broker.Receiver.java
License:Open Source License
public String init() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host);/*from w ww . j a v a 2s .c o m*/ factory.setPort(port); factory.setUsername(user); factory.setPassword(password); factory.setVirtualHost(vHost); factory.setConnectionTimeout(60); try { connection = factory.newConnection(); channel = connection.createChannel(); consumer = new QueueingConsumer(channel); receiveQueueName = channel.queueDeclare("", false, false, true, null).getQueue(); channel.basicConsume(receiveQueueName, true, consumer); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. return null; } return receiveQueueName; }
From source file:org.trianacode.TrianaCloud.Broker.RPCServer.java
License:Open Source License
public void init() throws ServletException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host);// w w w . j av a 2s.c o m factory.setPort(port); factory.setUsername(user); factory.setPassword(password); factory.setVirtualHost(vHost); factory.setConnectionTimeout(60); try { keepRunning = true; td = new TaskDAO(); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(rpc_queue_name, false, false, false, null); channel.basicQos(1); consumer = new QueueingConsumer(channel); channel.basicConsume(rpc_queue_name, false, consumer); logger.info("[x] Awaiting RPC requests"); } catch (Exception e) { ServletException se = new ServletException(e); logger.fatal("Something Happened!", se); throw se; } }
From source file:org.trianacode.TrianaCloud.Utils.RPCClient.java
License:Open Source License
public RPCClient() { try {/* ww w .java 2s . c om*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("zotac.toaster.dbyz.co.uk"); factory.setVirtualHost("trianacloud"); factory.setUsername("trianacloud"); factory.setPassword("trianacloud"); factory.setRequestedHeartbeat(60); connection = factory.newConnection(); channel = connection.createChannel(); replyQueueName = channel.queueDeclare().getQueue(); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); } catch (Exception e) { logger.fatal("Error connecting to Rabbit while initialising RPCClient", e); } }
From source file:org.trpr.mule.transport.rabbitmq.RabbitConnector.java
License:Apache License
/** * Abstract method implementation. Creates the AMQP connection * @see org.mule.transport.AbstractConnector#doConnect() */// w ww . j a v a2 s.c om protected void doConnect() throws Exception { if (connection == null) { int totalNumberOfNodes = rabbitMQConfigurations.size(); int tries = 0; while (tries <= totalNumberOfNodes) { int index = (lastUsedConnectionIndex + 1) % totalNumberOfNodes; RabbitMQConfiguration rabbitMQConfiguration = null; try { ConnectionFactory factory = new ConnectionFactory(); rabbitMQConfiguration = rabbitMQConfigurations.get(index); factory.setUsername(rabbitMQConfiguration.getUserName()); factory.setPassword(rabbitMQConfiguration.getPassword()); factory.setVirtualHost(rabbitMQConfiguration.getVirtualHost()); factory.setRequestedHeartbeat(rabbitMQConfiguration.getRequestHeartBeat()); factory.setHost(rabbitMQConfiguration.getHostName()); factory.setPort(rabbitMQConfiguration.getPortNumber()); connection = factory.newConnection(); lastUsedConnectionIndex = index; logger.info("Connection successfully created to configuration = " + rabbitMQConfiguration); return; } catch (Exception e) { logger.info("Failed to connect to Rabbit MQ Node. Configuration is " + rabbitMQConfiguration + ". Will try other configurations"); } tries++; } logger.error("Failed to connect to all configured Rabbit MQ nodes"); throw new Exception("Failed to connect to all configured Rabbit MQ nodes"); } }
From source file:org.trpr.platform.integration.impl.messaging.RabbitConnectionHolder.java
License:Apache License
/** * Helper method to create RabbitMQ {@link Connection} and {@link Channel} for the specified {@link RabbitMQRpcConfiguration} * @param configuration the RabbitMQRpcConfiguration or one of its sub-types to create connection objects for * @throws MessagingException in case of errors during connection & channel creation *///from w w w . ja v a2 s .c om private void createConnection(RabbitMQRpcConfiguration configuration) throws MessagingException { synchronized (this) { // all code blocks that mutate the connection and channel objects held by this class are synchronized ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(configuration.getUserName()); factory.setPassword(configuration.getPassword()); factory.setVirtualHost(configuration.getVirtualHost()); factory.setRequestedHeartbeat(configuration.getRequestHeartBeat()); factory.setHost(configuration.getHostName()); factory.setPort(configuration.getPortNumber()); try { // create the connection this.conn = factory.newConnection(); // add a shutdown listener to the newly created connection this.conn.addShutdownListener(this); // create the channel this.channel = this.conn.createChannel(); this.channel.exchangeDeclare(configuration.getExchangeName(), configuration.getExchangeType(), configuration.isDurable()); } catch (Exception e) { LOGGER.error("Error initializing RabbitMQ connection for : " + configuration.toString(), e); throw new MessagingException( "Error initializing RabbitMQ connection for : " + configuration.toString()); //not passing the root cause as it is logged here } } }
From source file:org.wso2.carbon.event.adapter.rabbitmq.internal.util.RabbitMQOutputEventAdapterPublisher.java
License:Open Source License
/** * <pre>/*from w ww .jav a2 s . co m*/ * Create a rabbitmq ConnectionFactory instance * </pre> * @param hostName * @param port * @param userName * @param password * @param virtualHost * @return */ private synchronized static ConnectionFactory getConnectionFactory(String hostName, int port, String userName, String password, String virtualHost) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); factory.setPort(port); factory.setUsername(userName); factory.setPassword(password); factory.setVirtualHost(virtualHost); /** * Add connection recovery logic * @author Sang-Cheon Park * @date 2015.07.16 */ /** * connection that will recover automatically */ factory.setAutomaticRecoveryEnabled(true); /** * attempt recovery every 5 seconds */ factory.setNetworkRecoveryInterval(5 * 1000); /** * wait maximum 10 seconds to connect(if connection failed, will be retry to connect after 5 seconds) */ factory.setConnectionTimeout(10 * 1000); return factory; }
From source file:org.wso2.carbon.event.adaptor.rabbitmq.internal.EventAdapterHelper.java
License:Apache License
/** * <pre>/*from w w w . j av a 2 s . c o m*/ * Create a rabbitmq ConnectionFactory instance * </pre> * @param hostName * @param port * @param userName * @param password * @param virtualHost * @return */ public synchronized static ConnectionFactory getConnectionFactory(String hostName, int port, String userName, String password, String virtualHost) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); factory.setPort(port); factory.setUsername(userName); factory.setPassword(password); factory.setVirtualHost(virtualHost); /** * Add connection recovery logic * @author Sang-Cheon Park * @date 2015.07.16 */ /** * connection that will recover automatically */ factory.setAutomaticRecoveryEnabled(true); /** * attempt recovery every 5 seconds */ factory.setNetworkRecoveryInterval(5 * 1000); /** * wait maximum 10 seconds to connect(if connection failed, will be retry to connect after 5 seconds) */ factory.setConnectionTimeout(10 * 1000); return factory; }
From source file:red.yelo.chat.AbstractRabbitMQConnector.java
License:Open Source License
/** * Connect to the broker and create the exchange * * @return success//from ww w . j a va 2 s .co m */ protected boolean connectToRabbitMQ(final String userName, final String password) { if ((mChannel != null) && mChannel.isOpen()) { return true; } try { final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(mServer); connectionFactory.setUsername(userName); connectionFactory.setPassword(password); connectionFactory.setVirtualHost(mVirtualHost); connectionFactory.setPort(mPort); // if (AbstractYeloActivity.mainActivityIsOpen()) { connectionFactory.setRequestedHeartbeat(AppConstants.HEART_BEAT); Logger.d(TAG, AppConstants.HEART_BEAT + ""); // } // else{ // connectionFactory.setRequestedHeartbeat(AppConstants.HEART_BEAT_BACKGROUND); // Logger.d(TAG,AppConstants.HEART_BEAT_BACKGROUND+""); // // } mConnection = connectionFactory.newConnection(); mChannel = mConnection.createChannel(); mChannel.exchangeDeclare(mExchange, mExchangeType.key); return true; } catch (final Exception e) { e.printStackTrace(); return false; } }
From source file:ru.kinomir.queue.QueueSender.java
public synchronized void sendToQueue(Object data, String queueName, String queueHost, String userName, String password, String port, String virtualHost) { Channel channel = null;//from ww w . j av a 2 s .c om Connection connection = null; try { logger.info("Send message to queue '" + queueName + "'"); ConnectionFactory factory = new ConnectionFactory(); if (!StringTools.isEmpty(userName)) { factory.setUsername(userName); } if (!StringTools.isEmpty(password)) { factory.setPassword(password); } if (!StringTools.isEmpty(port)) { try { factory.setPort(Integer.parseInt(port)); } catch (NumberFormatException ignore) { } } if (!StringTools.isEmpty(virtualHost)) { factory.setVirtualHost(virtualHost); } factory.setHost(queueHost); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, true, false, false, null); String message = convertToString(data); logger.info("Message text: " + message); channel.basicPublish("", queueName, MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes()); logger.info("Message was sent"); } catch (Exception ex) { logger.error("Uneble send message: " + convertToString(data)); logger.debug(ex.getMessage(), ex); } finally { try { if (channel != null) { channel.close(); } if (connection != null) { connection.close(); } } catch (Exception ignore) { } } }