List of usage examples for com.rabbitmq.client ConnectionFactory ConnectionFactory
ConnectionFactory
From source file:org.apache.nutch.indexwriter.rabbit.RabbitIndexWriter.java
License:Apache License
@Override public void open(JobConf JobConf, String name) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(serverHost);/*from ww w . j av a 2 s . c o m*/ factory.setPort(serverPort); if (serverVirtualHost != null) { factory.setVirtualHost(serverVirtualHost); } factory.setUsername(serverUsername); factory.setPassword(serverPassword); try { connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(exchangeServer, exchangeType, true); channel.queueDeclare(queueName, queueDurable, false, false, null); channel.queueBind(queueName, exchangeServer, queueRoutingKey); } catch (TimeoutException | IOException ex) { throw makeIOException(ex); } }
From source file:org.apache.nutch.publisher.rabbitmq.RabbitMQPublisherImpl.java
License:Apache License
@Override public boolean setConfig(Configuration conf) { try {//w w w . ja va2 s.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. */// w w w . java2 s .c om 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.apache.nutch.rabbitmq.RabbitMQClient.java
License:Apache License
/** * Builds a new instance of {@link RabbitMQClient} * * @param uri The connection parameters in the form amqp://userName:password@hostName:portNumber/virtualHost * @throws IOException It is thrown if there is some issue during the connection creation. */// w w w . ja v a 2s . com public RabbitMQClient(String uri) throws IOException { ConnectionFactory factory = new ConnectionFactory(); try { factory.setUri(uri); connection = factory.newConnection(); } catch (URISyntaxException | NoSuchAlgorithmException | KeyManagementException | TimeoutException e) { throw makeIOException(e); } }
From source file:org.apache.pig.piggybank.squeal.metrics.RMQMetricsTransport.java
License:Apache License
@Override public void initialize(Map props) { super.initialize(props); try {/* ww w . j ava2s. c om*/ rabbitURI = (String) props.get(RMQ_URI_KEY); factory = new ConnectionFactory(); factory.setUri(rabbitURI); connection = factory.newConnection(); channel = connection.createChannel(); exchangeName = (String) props.get(RMQ_EXCHANGE_KEY); channel.exchangeDeclare(exchangeName, "fanout", true); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Failed to setup writer", e); } }
From source file:org.apache.pig.piggybank.squeal.spout.RMQSpout.java
License:Apache License
public void connect() { try {//from w w w. ja v a 2 s. c o m factory = new ConnectionFactory(); factory.setUri(rabbitURI); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "fanout", true); if (QOS > 0) { channel.basicQos(QOS); } // TODO: Setup message-ttl so we don't gum up the works if things get bad. // Map<String, Object> args = new HashMap<String, Object>(); // args.put("x-message-ttl", 60000); String queueName; if (_queueName != null) { queueName = _queueName; channel.queueDeclare(queueName, true, false, false, null); } else { queueName = channel.queueDeclare().getQueue(); } channel.queueBind(queueName, exchangeName, ""); consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, false, consumer); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Failed to setup reader", e); } }
From source file:org.apache.pig.piggybank.storage.RMQStorage.java
License:Apache License
public void connect() { try {/*w w w. ja v a 2 s . c o m*/ factory = new ConnectionFactory(); factory.setUri(rabbitURI); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "fanout", true); connected = true; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Failed to setup reader", e); } }
From source file:org.apache.pulsar.connect.rabbitmq.RabbitMQSource.java
License:Apache License
@Override public void open(Map<String, String> config) throws Exception { rabbitMQConfig = RabbitMQConfig.load(config); if (rabbitMQConfig.getAmqUri() == null || rabbitMQConfig.getQueueName() == null) { throw new IllegalArgumentException("Required property not set."); }// www . j a v a 2 s .c o m ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(rabbitMQConfig.getAmqUri()); rabbitMQConnection = connectionFactory.newConnection(rabbitMQConfig.getConnectionName()); logger.info("A new connection to {}:{} has been opened successfully.", rabbitMQConnection.getAddress().getCanonicalHostName(), rabbitMQConnection.getPort()); rabbitMQChannel = rabbitMQConnection.createChannel(); rabbitMQChannel.queueDeclare(rabbitMQConfig.getQueueName(), false, false, false, null); com.rabbitmq.client.Consumer consumer = new RabbitMQConsumer(this.consumer, rabbitMQChannel); rabbitMQChannel.basicConsume(rabbitMQConfig.getQueueName(), consumer); logger.info("A consumer for queue {} has been successfully started.", rabbitMQConfig.getQueueName()); }
From source file:org.apache.pulsar.io.rabbitmq.RabbitMQSource.java
License:Apache License
@Override public void open(Map<String, Object> config, SourceContext sourceContext) throws Exception { rabbitMQConfig = RabbitMQConfig.load(config); if (rabbitMQConfig.getAmqUri() == null || rabbitMQConfig.getQueueName() == null) { throw new IllegalArgumentException("Required property not set."); }/* ww w .j a v a 2s . c o m*/ ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(rabbitMQConfig.getAmqUri()); rabbitMQConnection = connectionFactory.newConnection(rabbitMQConfig.getConnectionName()); logger.info("A new connection to {}:{} has been opened successfully.", rabbitMQConnection.getAddress().getCanonicalHostName(), rabbitMQConnection.getPort()); rabbitMQChannel = rabbitMQConnection.createChannel(); rabbitMQChannel.queueDeclare(rabbitMQConfig.getQueueName(), false, false, false, null); com.rabbitmq.client.Consumer consumer = new RabbitMQConsumer(this, rabbitMQChannel); rabbitMQChannel.basicConsume(rabbitMQConfig.getQueueName(), consumer); logger.info("A consumer for queue {} has been successfully started.", rabbitMQConfig.getQueueName()); }
From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQStore.java
License:Open Source License
private boolean initme() { Set<Map.Entry<String, Object>> mapSet = parameters.entrySet(); for (Map.Entry<String, Object> e : mapSet) { if (e.getValue() instanceof String) { properties.put(e.getKey(), e.getValue()); }/*from www . j a va 2s . c o m*/ } userName = (String) parameters.get(USERNAME); password = (String) parameters.get(PASSWORD); hostName = (String) parameters.get(HOST_NAME); hostPort = (String) parameters.get(HOST_PORT); virtualHost = (String) parameters.get(VIRTUAL_HOST); //Possible timeouts that can be added in future if requested, should be added to the //setConnectionTimeout, ShutdownTimeout, RequestedHeartbeat connectionFactory = new ConnectionFactory(); if (hostName != null && !hostName.equals("")) { connectionFactory.setHost(hostName); } else { throw new SynapseException(nameString() + " host name is not correctly defined"); } int port = 0; try { port = Integer.parseInt(hostPort); } catch (NumberFormatException nfe) { logger.error("Port value for " + nameString() + " is not correctly defined" + nfe); } if (port > 0) { connectionFactory.setPort(port); } else { connectionFactory.setPort(DEFAULT_PORT); logger.info(nameString() + " port is set to default value (5672"); } if (userName != null && !userName.equals("")) { connectionFactory.setUsername(userName); } if (password != null && !password.equals("")) { connectionFactory.setPassword(password); } if (virtualHost != null && !virtualHost.equals("")) { connectionFactory.setVirtualHost(virtualHost); } String sslEnabledS = parameters.get(SSL_ENABLED) != null ? parameters.get(SSL_ENABLED).toString() : ""; if (!StringUtils.isEmpty(sslEnabledS)) { try { boolean sslEnabled = Boolean.parseBoolean(sslEnabledS); if (sslEnabled) { String keyStoreLocation = parameters.get(SSL_KEYSTORE_LOCATION) != null ? parameters.get(SSL_KEYSTORE_LOCATION).toString() : ""; String keyStoreType = parameters.get(SSL_KEYSTORE_TYPE) != null ? parameters.get(SSL_KEYSTORE_TYPE).toString() : ""; String keyStorePassword = parameters.get(SSL_KEYSTORE_PASSWORD) != null ? parameters.get(SSL_KEYSTORE_PASSWORD).toString() : ""; String trustStoreLocation = parameters.get(SSL_TRUSTSTORE_LOCATION) != null ? parameters.get(SSL_TRUSTSTORE_LOCATION).toString() : ""; String trustStoreType = parameters.get(SSL_TRUSTSTORE_TYPE) != null ? parameters.get(SSL_TRUSTSTORE_TYPE).toString() : ""; String trustStorePassword = parameters.get(SSL_TRUSTSTORE_PASSWORD) != null ? parameters.get(SSL_TRUSTSTORE_PASSWORD).toString() : ""; String sslVersion = parameters.get(SSL_VERSION) != null ? parameters.get(SSL_VERSION).toString() : ""; if (StringUtils.isEmpty(keyStoreLocation) || StringUtils.isEmpty(keyStoreType) || StringUtils.isEmpty(keyStorePassword) || StringUtils.isEmpty(trustStoreLocation) || StringUtils.isEmpty(trustStoreType) || StringUtils.isEmpty(trustStorePassword)) { logger.warn( "Trustore and keystore information is not provided correctly. Proceeding with default SSL configuration"); connectionFactory.useSslProtocol(); } else { char[] keyPassphrase = keyStorePassword.toCharArray(); KeyStore ks = KeyStore.getInstance(keyStoreType); ks.load(new FileInputStream(keyStoreLocation), keyPassphrase); KeyManagerFactory kmf = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, keyPassphrase); char[] trustPassphrase = trustStorePassword.toCharArray(); KeyStore tks = KeyStore.getInstance(trustStoreType); tks.load(new FileInputStream(trustStoreLocation), trustPassphrase); TrustManagerFactory tmf = TrustManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); tmf.init(tks); SSLContext c = SSLContext.getInstance(sslVersion); c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); connectionFactory.useSslProtocol(c); } } } catch (Exception e) { logger.warn("Format error in SSL enabled value. Proceeding without enabling SSL", e); } } //declaring queue String queueName = (String) parameters.get(QUEUE_NAME); if (queueName != null) { this.queueName = queueName; } else { String name = getName(); String defaultQueue; if (name != null && !name.isEmpty()) { defaultQueue = name + "_Queue"; } else { defaultQueue = "RabiitmqStore_" + System.currentTimeMillis() + "_Queue"; } logger.warn(nameString() + ". Destination not provided. " + "Setting default destination to [" + defaultQueue + "]."); this.queueName = defaultQueue; } exchangeName = (String) properties.get(EXCHANGE_NAME); routeKey = (String) properties.get(ROUTE_KEY); if (routeKey == null) { logger.warn(nameString() + ". Routing key is not provided. " + "Setting queue name " + this.queueName + " as routing key."); routeKey = this.queueName; } if (!newProducerConnection()) { logger.warn(nameString() + ". Starting with a faulty connection to the broker."); return false; } try { setQueue(); } catch (IOException e) { logger.error(nameString() + " error in storage declaring queue " + queueName); return false; } return true; }