Package org.apache.qpid.client

Examples of org.apache.qpid.client.AMQConnection


        // make sure that failover timeout is bigger than flow control timeout
        setTestSystemProperty("qpid.failover_method_timeout", "60000");
        setTestSystemProperty("qpid.flow_control_wait_failure", "10000");

        AMQConnection connection = null;
        try
        {
            connection = createConnectionWithFailover();

            final Session producerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
            final Queue queue = createAndBindQueueWithFlowControlEnabled(producerSession, getTestQueueName(), DEFAULT_MESSAGE_SIZE * 3, DEFAULT_MESSAGE_SIZE * 2);
            final AtomicInteger counter = new AtomicInteger();
            // try to send 5 messages (should block after 4)
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        MessageProducer producer = producerSession.createProducer(queue);
                        for (int i=0; i < 5; i++)
                        {
                            Message next = createNextMessage(producerSession, i);
                            producer.send(next);
                            producerSession.commit();
                            counter.incrementAndGet();
                        }
                    }
                    catch(Exception e)
                    {
                        // ignore
                    }
                }
            }).start();

            long limit= 30000l;
            long start = System.currentTimeMillis();

            // wait  until session is blocked
            while(!((AMQSession<?,?>)producerSession).isFlowBlocked() && System.currentTimeMillis() - start < limit)
            {
                Thread.sleep(100l);
            }

            assertTrue("Flow is not blocked", ((AMQSession<?, ?>) producerSession).isFlowBlocked());
            // Message counter could be 3 or 4 depending on the progression of producing thread relative
            // to the receipt of the ChannelFlow.
            final int currentCounter = counter.get();
            assertTrue("Unexpected number of sent messages", currentCounter == 3 || currentCounter == 4);

            killBroker();
            startBroker();

            // allows the failover thread to proceed
            Thread.yield();
            awaitForFailoverCompletion(60000l);

            assertFalse("Flow is blocked", ((AMQSession<?, ?>) producerSession).isFlowBlocked());
        }
        finally
        {
            if (connection != null)
            {
                connection.close();
            }
        }
    }
View Full Code Here


        return queue;
    }

    private AMQConnection createConnectionWithFailover() throws NamingException, JMSException
    {
        AMQConnection connection;
        AMQConnectionFactory connectionFactory = (AMQConnectionFactory)getConnectionFactory("default");
        ConnectionURL connectionURL = connectionFactory.getConnectionURL();
        connectionURL.setOption(ConnectionURL.OPTIONS_FAILOVER, "singlebroker");
        connectionURL.setOption(ConnectionURL.OPTIONS_FAILOVER_CYCLE, "2");
        BrokerDetails details = connectionURL.getBrokerDetails(0);
        details.setProperty(BrokerDetails.OPTIONS_RETRY, "200");
        details.setProperty(BrokerDetails.OPTIONS_CONNECT_DELAY, "1000");

        connection = (AMQConnection)connectionFactory.createConnection("admin", "admin");
        connection.setConnectionListener(this);
        return connection;
    }
View Full Code Here

        return setDelayedFailoverPolicy(2);
    }

    private DelayingFailoverPolicy setDelayedFailoverPolicy(long delay)
    {
        AMQConnection amqConnection = (AMQConnection) _connection;
        DelayingFailoverPolicy failoverPolicy = new DelayingFailoverPolicy(amqConnection, delay);
        ((AMQConnection) _connection).setFailoverPolicy(failoverPolicy);
        return failoverPolicy;
    }
View Full Code Here

    private String _broker_BadDNS = "tcp://hg3sgaaw4lgihjs";

    public void testSimpleConnection() throws Exception
    {
        AMQConnection conn = null;
        try
        {
            conn = new AMQConnection(getBroker().toString(), "guest", "guest", "fred", "test");
        }
        catch (Exception e)
        {
            fail("Connection to " + getBroker() + " should succeed. Reason: " + e);
        }
        finally
        {
            if(conn != null)
            {
                conn.close();
            }
        }
    }
View Full Code Here

        }
    }

    public void testDefaultExchanges() throws Exception
    {
        AMQConnection conn = null;
        try
        {
            BrokerDetails broker = getBroker();
            broker.setProperty(BrokerDetails.OPTIONS_RETRY, "1");
            ConnectionURL url = new AMQConnectionURL("amqp://guest:guest@clientid/test?brokerlist='"
                                     + broker
                                     + "'&defaultQueueExchange='test.direct'"
                                     + "&defaultTopicExchange='test.topic'"
                                     + "&temporaryQueueExchange='tmp.direct'"
                                     + "&temporaryTopicExchange='tmp.topic'");

            System.err.println(url.toString());
            conn = new AMQConnection(url);


            AMQSession sess = (AMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

            sess.declareExchange(new AMQShortString("test.direct"),
                    AMQShortString.valueOf(ExchangeDefaults.DIRECT_EXCHANGE_CLASS), false);

            sess.declareExchange(new AMQShortString("tmp.direct"),
                    AMQShortString.valueOf(ExchangeDefaults.DIRECT_EXCHANGE_CLASS), false);

            sess.declareExchange(new AMQShortString("tmp.topic"),
                    AMQShortString.valueOf(ExchangeDefaults.TOPIC_EXCHANGE_CLASS), false);

            sess.declareExchange(new AMQShortString("test.topic"),
                    AMQShortString.valueOf(ExchangeDefaults.TOPIC_EXCHANGE_CLASS), false);

            QueueSession queueSession = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            AMQQueue queue = (AMQQueue) queueSession.createQueue("MyQueue");

            assertEquals(queue.getExchangeName().toString(), "test.direct");

            AMQQueue tempQueue = (AMQQueue) queueSession.createTemporaryQueue();

            assertEquals(tempQueue.getExchangeName().toString(), "tmp.direct");

            queueSession.close();

            TopicSession topicSession = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

            AMQTopic topic = (AMQTopic) topicSession.createTopic("silly.topic");

            assertEquals(topic.getExchangeName().toString(), "test.topic");

            AMQTopic tempTopic = (AMQTopic) topicSession.createTemporaryTopic();

            assertEquals(tempTopic.getExchangeName().toString(), "tmp.topic");

            topicSession.close();

        }
        catch (Exception e)
        {
            fail("Connection to " + getBroker() + " should succeed. Reason: " + e);
        }
        finally
        {
            conn.close();
        }
    }
View Full Code Here

        }
    }

    public void testPasswordFailureConnection() throws Exception
    {
        AMQConnection conn = null;
        try
        {
            BrokerDetails broker = getBroker();
            broker.setProperty(BrokerDetails.OPTIONS_RETRY, "0");
            conn = new AMQConnection("amqp://guest:rubbishpassword@clientid/test?brokerlist='" + broker + "'");
            fail("Connection should not be established password is wrong.");
        }
        catch (AMQConnectionFailureException amqe)
        {
            assertNotNull("No cause set:" + amqe.getMessage(), amqe.getCause());
            assertTrue("Exception was wrong type", amqe.getCause() instanceof AMQException);
        }
        finally
        {
            if (conn != null)
            {
                conn.close();
            }
        }
    }
View Full Code Here

        }
    }

    public void testConnectionFailure() throws Exception
    {
        AMQConnection conn = null;
        try
        {
            conn = new AMQConnection("amqp://guest:guest@clientid/testpath?brokerlist='" + _broker_NotRunning + "?retries='0''");
            fail("Connection should not be established");
        }
        catch (AMQException amqe)
        {
            if (!(amqe instanceof AMQConnectionFailureException))
            {
                fail("Correct exception not thrown. Excpected 'AMQConnectionException' got: " + amqe);
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.close();
            }
        }

    }
View Full Code Here

    }

    public void testUnresolvedHostFailure() throws Exception
    {
        AMQConnection conn = null;
        try
        {
            conn = new AMQConnection("amqp://guest:guest@clientid/testpath?brokerlist='" + _broker_BadDNS + "?retries='0''");
            fail("Connection should not be established");
        }
        catch (AMQException amqe)
        {
            if (!(amqe instanceof AMQUnresolvedAddressException))
            {
                fail("Correct exception not thrown. Excpected 'AMQUnresolvedAddressException' got: " + amqe);
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.close();
            }
        }

    }
View Full Code Here

    }

    public void testUnresolvedVirtualHostFailure() throws Exception
    {
        AMQConnection conn = null;
        try
        {
            BrokerDetails broker = getBroker();
            broker.setProperty(BrokerDetails.OPTIONS_RETRY, "0");
            conn = new AMQConnection("amqp://guest:guest@clientid/rubbishhost?brokerlist='" + broker + "'");
            fail("Connection should not be established");
        }
        catch (AMQException amqe)
        {
            if (!(amqe instanceof AMQConnectionFailureException))
            {
                fail("Correct exception not thrown. Excpected 'AMQConnectionFailureException' got: " + amqe);
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.close();
            }
        }
    }
View Full Code Here

        }
    }

    public void testClientIdCannotBeChanged() throws Exception
    {
        Connection connection = new AMQConnection(getBroker().toString(), "guest", "guest",
                                                  "fred", "test");
        try
        {
            connection.setClientID("someClientId");
            fail("No IllegalStateException thrown when resetting clientid");
        }
        catch (javax.jms.IllegalStateException e)
        {
            // PASS
        }
        finally
        {
            if (connection != null)
            {
                connection.close();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.qpid.client.AMQConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.