Search in sources :

Example 26 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method crazyProtocolHeader.

@Test
public void crazyProtocolHeader() throws IOException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    // keep the frame handler's socket
    Socket fhSocket = SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT);
    SocketFrameHandler fh = new SocketFrameHandler(fhSocket);
    // major, minor
    fh.sendHeader(100, 3);
    DataInputStream in = fh.getInputStream();
    // we should get a valid protocol header back
    byte[] header = new byte[4];
    in.read(header);
    // The protocol header is "AMQP" plus a version that the server
    // supports.  We can really only test for the first bit.
    assertEquals("AMQP", new String(header));
    in.read(header);
    assertEquals(in.available(), 0);
    // gives an error.
    if (!fhSocket.isClosed()) {
        fh.setTimeout(500);
        // NB the frame handler will return null if the socket times out
        try {
            fh.readFrame();
            fail("Expected socket read to fail due to socket being closed");
        } catch (MalformedFrameException mfe) {
            fail("Expected nothing, rather than a badly-formed something");
        } catch (IOException ioe) {
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) MalformedFrameException(com.rabbitmq.client.MalformedFrameException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) Socket(java.net.Socket) Test(org.junit.Test)

Example 27 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method correctProtocolHeader.

@Test
public void correctProtocolHeader() throws IOException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    SocketFrameHandler fh = new SocketFrameHandler(SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT));
    fh.sendHeader();
    AMQCommand command = new AMQCommand();
    while (!command.handleFrame(fh.readFrame())) {
    }
    Method m = command.getMethod();
    // System.out.println(m.getClass());
    assertTrue("First command must be Connection.start", m instanceof AMQP.Connection.Start);
    AMQP.Connection.Start start = (AMQP.Connection.Start) m;
    assertTrue("Version in Connection.start is <= what we sent", start.getVersionMajor() < AMQP.PROTOCOL.MAJOR || (start.getVersionMajor() == AMQP.PROTOCOL.MAJOR && start.getVersionMinor() <= AMQP.PROTOCOL.MINOR));
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQP(com.rabbitmq.client.AMQP) Method(com.rabbitmq.client.Method) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) AMQCommand(com.rabbitmq.client.impl.AMQCommand) Test(org.junit.Test)

Example 28 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method frameMaxLessThanFrameMinSize.

@Test
public void frameMaxLessThanFrameMinSize() throws IOException, TimeoutException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    factory.setRequestedFrameMax(100);
    try {
        factory.newConnection();
    } catch (IOException ioe) {
        return;
    }
    fail("Broker should have closed the connection since our frame max < frame_min_size");
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException) Test(org.junit.Test)

Example 29 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class ExceptionHandling method testConsumerHandleConsumerException.

protected void testConsumerHandleConsumerException(ExceptionHandler eh, CountDownLatch latch, boolean expectChannelClose) throws InterruptedException, TimeoutException, IOException {
    ConnectionFactory cf = newConnectionFactory(eh);
    assertEquals(cf.getExceptionHandler(), eh);
    Connection conn = cf.newConnection();
    assertEquals(conn.getExceptionHandler(), eh);
    Channel ch = conn.createChannel();
    String q = ch.queueDeclare().getQueue();
    ch.basicConsume(q, new DefaultConsumer(ch) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            throw new RuntimeException("exception expected here, don't freak out");
        }
    });
    ch.basicPublish("", q, null, "".getBytes());
    wait(latch);
    assertEquals(!expectChannelClose, ch.isOpen());
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) AMQP(com.rabbitmq.client.AMQP) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope)

Example 30 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class ExceptionHandling method nullExceptionHandler.

@Test
public void nullExceptionHandler() {
    ConnectionFactory cf = TestUtils.connectionFactory();
    try {
        cf.setExceptionHandler(null);
        fail("expected setExceptionHandler to throw");
    } catch (IllegalArgumentException iae) {
    // expected
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)64 Connection (com.rabbitmq.client.Connection)28 Test (org.junit.Test)26 IOException (java.io.IOException)17 Channel (com.rabbitmq.client.Channel)13 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)3 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)3 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 TimeoutException (java.util.concurrent.TimeoutException)3 AMQP (com.rabbitmq.client.AMQP)2 Address (com.rabbitmq.client.Address)2 Consumer (com.rabbitmq.client.Consumer)2 DnsRecordIpAddressResolver (com.rabbitmq.client.DnsRecordIpAddressResolver)2 MetricsCollector (com.rabbitmq.client.MetricsCollector)2 ShutdownListener (com.rabbitmq.client.ShutdownListener)2 SslContextFactory (com.rabbitmq.client.SslContextFactory)2