List of usage examples for com.rabbitmq.client ConnectionFactory setPassword
public void setPassword(String password)
From source file:org.apache.nutch.publisher.rabbitmq.RabbitMQPublisherImpl.java
License:Apache License
@Override public boolean setConfig(Configuration conf) { try {//from w w w .ja v a2s. c o m EXCHANGE_SERVER = conf.get("rabbitmq.exchange.server", "fetcher_log"); EXCHANGE_TYPE = conf.get("rabbitmq.exchange.type", "fanout"); HOST = conf.get("rabbitmq.host", "localhost"); PORT = conf.getInt("rabbitmq.port", 5672); VIRTUAL_HOST = conf.get("rabbitmq.virtualhost", null); USERNAME = conf.get("rabbitmq.username", null); PASSWORD = conf.get("rabbitmq.password", null); QUEUE_NAME = conf.get("rabbitmq.queue.name", "fanout.queue"); QUEUE_DURABLE = conf.getBoolean("rabbitmq.queue.durable", true); QUEUE_ROUTING_KEY = conf.get("rabbitmq.queue.routingkey", "fanout.key"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST); factory.setPort(PORT); if (VIRTUAL_HOST != null) { factory.setVirtualHost(VIRTUAL_HOST); } if (USERNAME != null) { factory.setUsername(USERNAME); factory.setPassword(PASSWORD); } Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_SERVER, EXCHANGE_TYPE); channel.queueDeclare(QUEUE_NAME, QUEUE_DURABLE, false, false, null); channel.queueBind(QUEUE_NAME, EXCHANGE_SERVER, QUEUE_ROUTING_KEY); LOG.info("Configured RabbitMQ publisher"); return true; } catch (Exception e) { LOG.error("Could not initialize RabbitMQ publisher - {}", StringUtils.stringifyException(e)); return false; } }
From source file:org.apache.nutch.rabbitmq.RabbitMQClient.java
License:Apache License
/** * Builds a new instance of {@link RabbitMQClient} * * @param serverHost The server host. * @param serverPort The server port. * @param serverVirtualHost The virtual host into the RabbitMQ server. * @param serverUsername The username to access the server. * @param serverPassword The password to access the server. * @throws IOException It is thrown if there is some issue during the connection creation. *///from w w w . j ava 2 s . c o m public RabbitMQClient(String serverHost, int serverPort, String serverVirtualHost, String serverUsername, String serverPassword) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(getValue(serverHost, "localhost")); factory.setPort(getValue(serverPort, 5672)); factory.setVirtualHost(getValue(serverVirtualHost, "/")); factory.setUsername(getValue(serverUsername, "guest")); factory.setPassword(getValue(serverPassword, "guest")); try { connection = factory.newConnection(); } catch (TimeoutException e) { throw makeIOException(e); } }
From source file:org.ballerinalang.messaging.rabbitmq.util.ConnectionUtils.java
License:Open Source License
/** * Creates a RabbitMQ Connection using the given connection parameters. * * @param connectionConfig Parameters used to initialize the connection. * @return RabbitMQ Connection object.// w ww . j ava2 s .c o m */ public static Connection createConnection(BMap<String, BValue> connectionConfig) { try { ConnectionFactory connectionFactory = new ConnectionFactory(); String host = RabbitMQUtils.getStringFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_HOST); connectionFactory.setHost(host); int port = RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_PORT, LOGGER); connectionFactory.setPort(port); if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_USER) != null) { connectionFactory.setUsername(RabbitMQUtils.getStringFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_USER)); } if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_PASS) != null) { connectionFactory.setPassword(RabbitMQUtils.getStringFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_PASS)); } if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_TIMEOUT) != null) { connectionFactory.setConnectionTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_TIMEOUT, LOGGER)); } if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_HANDSHAKE_TIMEOUT) != null) { connectionFactory.setHandshakeTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_HANDSHAKE_TIMEOUT, LOGGER)); } if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_SHUTDOWN_TIMEOUT) != null) { connectionFactory.setShutdownTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_SHUTDOWN_TIMEOUT, LOGGER)); } if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_HEARTBEAT) != null) { connectionFactory.setRequestedHeartbeat(RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_HEARTBEAT, LOGGER)); } return connectionFactory.newConnection(); } catch (IOException | TimeoutException exception) { LOGGER.error(RabbitMQConstants.CREATE_CONNECTION_ERROR, exception); throw new RabbitMQConnectorException(RabbitMQConstants.CREATE_CONNECTION_ERROR + exception.getMessage(), exception); } }
From source file:org.belio.mq.RabbitConsumer.java
@Override public void open() throws IOException { try {/*from ww w . ja va2 s. c o m*/ com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setHost(mqproperties.getProperty("host")); factory.setVirtualHost(mqproperties.getProperty("vhost")); factory.setUsername(mqproperties.getProperty("username")); factory.setPassword(mqproperties.getProperty("password")); factory.setPort(Integer.parseInt(mqproperties.getProperty("port"))); factory.setAutomaticRecoveryEnabled(true); factory.setNetworkRecoveryInterval(1); // Create a new connection to MQ connection = factory.newConnection(); // Create a new channel and declare it's type and exhange as well //Create a new rabbit publisher executor = Executors.newScheduledThreadPool( Integer.parseInt(threadproperties.getProperty(queueType.name().toLowerCase()).split(",")[0])); } catch (IOException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } catch (ShutdownSignalException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } catch (ConsumerCancelledException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.belio.mq.RabbitConsumer.java
private void openPublisher() { try {//from w ww .j av a 2 s . c om ConnectionFactory factory = new ConnectionFactory(); factory.setHost(mqproperties.getProperty("host")); factory.setVirtualHost(mqproperties.getProperty("vhost")); factory.setUsername(mqproperties.getProperty("username")); factory.setPassword(mqproperties.getProperty("password")); factory.setPort(Integer.parseInt(mqproperties.getProperty("port"))); // Create a new connection to MQ publishingConnection = factory.newConnection(); // Create a new channel and declare it's type and exhange as well publishingChannel = connection.createChannel(); publishingChannel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null); publishingChannel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX), mqproperties.getProperty("type")); publishingChannel.queueBind(queueType.name().concat(QUEUE_SUFFIX), queueType.name().concat(EXCHANGE_SUFFIX), ""); } catch (IOException exception) { Launcher.LOG.error(exception.getLocalizedMessage(), exception); } }
From source file:org.belio.mq.RabbitPublisher.java
@Override public void open() throws IOException { synchronized (RabbitPublisher.class) { try {/*from www .j av a 2s. c om*/ // Create a connection factory and hosts = mqproperties.getProperty("host").split(","); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(mqproperties.getProperty("host")); factory.setVirtualHost(mqproperties.getProperty("vhost")); factory.setUsername(mqproperties.getProperty("username")); factory.setPassword(mqproperties.getProperty("password")); factory.setPort(Integer.parseInt(mqproperties.getProperty("port"))); // Create a new connection to MQ connection = factory.newConnection(); // Create a new channel and declare it's type and exhange as well channel = connection.createChannel(); channel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null); channel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX), mqproperties.getProperty("type")); channel.queueBind(queueType.name().concat(QUEUE_SUFFIX), queueType.name().concat(EXCHANGE_SUFFIX), ""); } catch (Exception ex) { ex.printStackTrace(); } } }
From source file:org.graylog2.inputs.amqp.AMQPConsumer.java
License:Open Source License
private Channel connect() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(server.getConfiguration().getAmqpUsername()); factory.setPassword(server.getConfiguration().getAmqpPassword()); factory.setVirtualHost(server.getConfiguration().getAmqpVirtualhost()); factory.setHost(server.getConfiguration().getAmqpHost()); factory.setPort(server.getConfiguration().getAmqpPort()); connection = factory.newConnection(Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat("amqp-consumer-" + queueConfig.getId() + "-%d").build())); return connection.createChannel(); }
From source file:org.graylog2.inputs.amqp.Consumer.java
License:Open Source License
public void connect() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname);/*from w w w. j a v a2 s.c o m*/ factory.setPort(port); // Authenticate? if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { factory.setUsername(username); factory.setPassword(password); } connection = factory.newConnection(); channel = connection.createChannel(); if (prefetchCount > 0) { channel.basicQos(prefetchCount); LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount); } connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { while (true) { try { LOG.error("AMQP connection lost! Trying reconnect in 1 second."); Thread.sleep(1000); connect(); LOG.info("Connected! Re-starting consumer."); run(); LOG.info("Consumer running."); break; } catch (IOException e) { LOG.error("Could not re-connect to AMQP broker.", e); } catch (InterruptedException ignored) { } } } }); }
From source file:org.graylog2.inputs.transports.AmqpConsumer.java
License:Open Source License
public void connect() throws IOException { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname);// w w w . ja va 2 s . c o m factory.setPort(port); factory.setVirtualHost(virtualHost); if (tls) { try { LOG.info("Enabling TLS for AMQP input [{}/{}].", sourceInput.getName(), sourceInput.getId()); factory.useSslProtocol(); } catch (NoSuchAlgorithmException | KeyManagementException e) { throw new IOException("Couldn't enable TLS for AMQP input.", e); } } // Authenticate? if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) { factory.setUsername(username); factory.setPassword(password); } connection = factory.newConnection(); channel = connection.createChannel(); if (null == channel) { LOG.error("No channel descriptor available!"); } if (null != channel && prefetchCount > 0) { channel.basicQos(prefetchCount); LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount); } connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { if (cause.isInitiatedByApplication()) { LOG.info("Not reconnecting connection, we disconnected explicitly."); return; } while (true) { try { LOG.error("AMQP connection lost! Trying reconnect in 1 second."); Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); connect(); LOG.info("Connected! Re-starting consumer."); run(); LOG.info("Consumer running."); break; } catch (IOException e) { LOG.error("Could not re-connect to AMQP broker.", e); } } } }); }
From source file:org.graylog2.messagehandlers.amqp.AMQPBroker.java
License:Open Source License
public Connection getConnection() throws IOException { if (connection == null || !connection.isOpen()) { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(getUsername()); factory.setPassword(getPassword()); factory.setVirtualHost(getVirtualHost()); factory.setHost(getHost());/*from w w w . ja va 2 s .c o m*/ factory.setPort(getPort()); this.connection = factory.newConnection(); } return this.connection; }