Search in sources :

Example 1 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project camel by apache.

the class RabbitMQEndpointTest method testCreateConnectionFactoryCustom.

@Test
public void testCreateConnectionFactoryCustom() throws Exception {
    ConnectionFactory connectionFactory = createConnectionFactory("rabbitmq:localhost:1234/exchange" + "?username=userxxx" + "&password=passxxx" + "&connectionTimeout=123" + "&requestedChannelMax=456" + "&requestedFrameMax=789" + "&requestedHeartbeat=987" + "&sslProtocol=true" + "&automaticRecoveryEnabled=true" + "&networkRecoveryInterval=654" + "&topologyRecoveryEnabled=false");
    assertEquals("localhost", connectionFactory.getHost());
    assertEquals(1234, connectionFactory.getPort());
    assertEquals("userxxx", connectionFactory.getUsername());
    assertEquals("passxxx", connectionFactory.getPassword());
    assertEquals(123, connectionFactory.getConnectionTimeout());
    assertEquals(456, connectionFactory.getRequestedChannelMax());
    assertEquals(789, connectionFactory.getRequestedFrameMax());
    assertEquals(987, connectionFactory.getRequestedHeartbeat());
    assertTrue(connectionFactory.isSSL());
    assertTrue(connectionFactory.isAutomaticRecoveryEnabled());
    assertEquals(654, connectionFactory.getNetworkRecoveryInterval());
    assertFalse(connectionFactory.isTopologyRecoveryEnabled());
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Example 2 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project camel by apache.

the class RabbitMQComponent method createEndpoint.

@Override
protected RabbitMQEndpoint createEndpoint(String uri, String remaining, Map<String, Object> params) throws Exception {
    URI host = new URI("http://" + remaining);
    String hostname = host.getHost();
    int portNumber = host.getPort();
    // We need to support the exchange to be "" the path is empty
    String exchangeName = "";
    if (host.getPath().trim().length() > 1) {
        exchangeName = host.getPath().substring(1);
    }
    // ConnectionFactory reference
    ConnectionFactory connectionFactory = resolveAndRemoveReferenceParameter(params, "connectionFactory", ConnectionFactory.class);
    @SuppressWarnings("unchecked") Map<String, Object> clientProperties = resolveAndRemoveReferenceParameter(params, "clientProperties", Map.class);
    TrustManager trustManager = resolveAndRemoveReferenceParameter(params, "trustManager", TrustManager.class);
    RabbitMQEndpoint endpoint;
    if (connectionFactory == null) {
        endpoint = new RabbitMQEndpoint(uri, this);
    } else {
        endpoint = new RabbitMQEndpoint(uri, this, connectionFactory);
    }
    endpoint.setHostname(hostname);
    endpoint.setPortNumber(portNumber);
    endpoint.setExchangeName(exchangeName);
    endpoint.setClientProperties(clientProperties);
    endpoint.setTrustManager(trustManager);
    setProperties(endpoint, params);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating RabbitMQEndpoint with host {}:{} and exchangeName: {}", new Object[] { endpoint.getHostname(), endpoint.getPortNumber(), endpoint.getExchangeName() });
    }
    HashMap<String, Object> args = new HashMap<>();
    args.putAll(IntrospectionSupport.extractProperties(params, ARG_PREFIX));
    endpoint.setArgs(args);
    HashMap<String, Object> argsCopy = new HashMap<>(args);
    // Combine the three types of rabbit arguments with their individual endpoint properties
    endpoint.getExchangeArgs().putAll(IntrospectionSupport.extractProperties(argsCopy, EXCHANGE_ARG_PREFIX));
    endpoint.getQueueArgs().putAll(IntrospectionSupport.extractProperties(argsCopy, QUEUE_ARG_PREFIX));
    endpoint.getBindingArgs().putAll(IntrospectionSupport.extractProperties(argsCopy, BINDING_ARG_PREFIX));
    return endpoint;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) HashMap(java.util.HashMap) URI(java.net.URI) TrustManager(javax.net.ssl.TrustManager)

Example 3 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project camel by apache.

the class RabbitMQConnectionFactorySupport method createFactoryFor.

public ConnectionFactory createFactoryFor(final RabbitMQEndpoint endpoint) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(endpoint.getUsername());
    factory.setPassword(endpoint.getPassword());
    factory.setVirtualHost(endpoint.getVhost());
    factory.setHost(endpoint.getHostname());
    factory.setPort(endpoint.getPortNumber());
    if (endpoint.getClientProperties() != null) {
        factory.setClientProperties(endpoint.getClientProperties());
    }
    factory.setConnectionTimeout(endpoint.getConnectionTimeout());
    factory.setRequestedChannelMax(endpoint.getRequestedChannelMax());
    factory.setRequestedFrameMax(endpoint.getRequestedFrameMax());
    factory.setRequestedHeartbeat(endpoint.getRequestedHeartbeat());
    if (endpoint.getSslProtocol() != null) {
        try {
            if (endpoint.getSslProtocol().equals("true")) {
                factory.useSslProtocol();
            } else if (endpoint.getTrustManager() == null) {
                factory.useSslProtocol(endpoint.getSslProtocol());
            } else {
                factory.useSslProtocol(endpoint.getSslProtocol(), endpoint.getTrustManager());
            }
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IllegalArgumentException("Invalid sslProtocol " + endpoint.getSslProtocol(), e);
        }
    }
    if (endpoint.getAutomaticRecoveryEnabled() != null) {
        factory.setAutomaticRecoveryEnabled(endpoint.getAutomaticRecoveryEnabled());
    }
    if (endpoint.getNetworkRecoveryInterval() != null) {
        factory.setNetworkRecoveryInterval(endpoint.getNetworkRecoveryInterval());
    }
    if (endpoint.getTopologyRecoveryEnabled() != null) {
        factory.setTopologyRecoveryEnabled(endpoint.getTopologyRecoveryEnabled());
    }
    return factory;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException)

Example 4 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project camel by apache.

the class AbstractRabbitMQIntTest method connection.

/**
     * Helper method for creating a rabbitmq connection to the test instance of the
     * rabbitmq server.
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
protected Connection connection() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5672);
    factory.setUsername("cameltest");
    factory.setPassword("cameltest");
    factory.setVirtualHost("/");
    return factory.newConnection();
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory)

Example 5 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project uavstack by uavorg.

the class DoTestRabbitmqProxy method main.

public static void main(String[] args) {
    ConsoleLogger cl = new ConsoleLogger("test");
    cl.setDebugable(true);
    UAVServer.instance().setLog(cl);
    UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR, ServerVendor.TOMCAT);
    RabbitmqHookProxy p = new RabbitmqHookProxy("test", Collections.emptyMap());
    p.doInstallDProxy(null, "testApp");
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setHost("127.0.0.1");
    factory.setPort(5672);
    try {
        conn = factory.newConnection();
        channel = conn.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueDeclare("aaa", false, false, false, null);
        new Thread(new Runnable() {

            @Override
            public void run() {
                String message = "Hello World!";
                while (true) {
                    try {
                        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
                        // System.out.println(" [x] Sent '" + message + "'");
                        channel.basicPublish("", "aaa", null, "aaame".getBytes("UTF-8"));
                        // System.out.println(" [x] Sent 'aaame'");
                        Thread.sleep(1000);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        Connection connection = factory.newConnection();
        Channel recvchannel = connection.createChannel();
        recvchannel.queueDeclare(QUEUE_NAME, false, false, false, null);
        recvchannel.queueDeclare("aaa", false, false, false, null);
        Consumer consumer = new DefaultConsumer(recvchannel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
            // System.out.println(" [x] Received '" + message + "'1");
            }
        };
        recvchannel.basicConsume(QUEUE_NAME, true, consumer);
        String consumerTag = recvchannel.basicConsume("aaa", true, consumer);
        try {
            Thread.sleep(50000);
            recvchannel.basicCancel(consumerTag);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) RabbitmqHookProxy(com.creditease.uav.hook.rabbitmq.RabbitmqHookProxy) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) ConsoleLogger(com.creditease.monitor.log.ConsoleLogger) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)188 Connection (com.rabbitmq.client.Connection)87 Test (org.junit.Test)73 Channel (com.rabbitmq.client.Channel)63 IOException (java.io.IOException)53 TimeoutException (java.util.concurrent.TimeoutException)23 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)11 Envelope (com.rabbitmq.client.Envelope)11 SSLContext (javax.net.ssl.SSLContext)11 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)9 HashMap (java.util.HashMap)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AMQP (com.rabbitmq.client.AMQP)6 Consumer (com.rabbitmq.client.Consumer)6 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)6 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 Address (com.rabbitmq.client.Address)5 NioParams (com.rabbitmq.client.impl.nio.NioParams)4 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 Path (javax.ws.rs.Path)3