You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Ricardo Melo <ri...@cflex.com.br> on 2010/01/04 16:26:08 UTC

Issue trying to browse a activemq queue

Hi,

I'm using camel and I need to get the number of elements present in a
activemq queue, because the queue has a max size.

I'm trying to use QueueBrowser Interface, with no success. The queue I put
messages in is called "activemq:queue:out".

I've found some code to do that job, but I can't connect to the queue, a
javax.naming.
NoInitialContextException is thrown at the identified line showed bellow.
The connection string is not working. What should I use to connect to the
queue created by camel?

Here is the code:

        // get the initial context
        InitialContext ctx = new InitialContext();

        // lookup the queue object
        [EXCEPTION!] Queue queue = (Queue) ctx.lookup("queue:out");

        // lookup the queue connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
        lookup("queue/connectionFactory");

        // create a queue connection
        QueueConnection queueConn = connFactory.createQueueConnection();

        // create a queue session
        QueueSession queueSession = queueConn.createQueueSession(false,
        Session.AUTO_ACKNOWLEDGE);

        // create a queue browser
        QueueBrowser queueBrowser = queueSession.createBrowser(queue);

        // start the connection
        queueConn.start();

Thanks in advance,
Ricardo Melo

Re: Issue trying to browse a activemq queue

Posted by boday <bo...@vektrel.com>.
I just use JMX to get these statistics...

here is some raw code to create a connection and iterate over the AMQ queues
to find a given queues size...

note: be careful to close/resuse JMX connections or they will cause
OutOfMemoryExceptions on the MBeanServer

...

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" +
machineName + ":" + port
					+ "/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
conn = jmxc.getMBeanServerConnection();

ObjectName activeMQ = new
ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
BrokerViewMBean mbean = (BrokerViewMBean)
MBeanServerInvocationHandler.newProxyInstance(mbsc, activeMQ,
BrokerViewMBean.class, true);

//find the queue in question and return the size
for (ObjectName name : mbean.getQueues())
{
	QueueViewMBean queueMbean = (QueueViewMBean)
MBeanServerInvocationHandler.newProxyInstance(mbsc,
name,QueueViewMBean.class, true);
       if (queueMbean.getName().equals(queueName))
	{
	      return queueMbean.getQueueSize();
	}
}


-----
Ben - Senior Consultant
using SMX 3.3.1/Camel 2.1
-- 
View this message in context: http://old.nabble.com/Issue-trying-to-browse-a-activemq-queue-tp27014479p27026944.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Issue trying to browse a activemq queue

Posted by James Strachan <ja...@gmail.com>.
2010/1/4 Ricardo Melo <ri...@cflex.com.br>:
> Thank you all fot the answers.
>
> It works now with the Browsable endpoint, thanks!
>
> Very clean and elegant solution.

Glad it worked :)

BTW as Claus mentions, its actually inefficient under the covers if
all you want is the size as it basically loads all the messages into
RAM one by one to get the size. We should add an ActiveMQ Camel
component optimisation of the List.size() method one day so that the
implementation just uses the ActiveMQ statistics to find out the size
of the queue without actually iterating through the list.

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Issue trying to browse a activemq queue

Posted by Ricardo Melo <ri...@cflex.com.br>.
Thank you all fot the answers.

It works now with the Browsable endpoint, thanks!

Very clean and elegant solution.

On Mon, Jan 4, 2010 at 2:33 PM, James Strachan <ja...@gmail.com>wrote:

> BTW it looks like you are confusing the queue name in JMS with the
> JNDI name in JNDI.
>
> Incidentally a simpler alternative to using the JNDI API to lookup the
> queue then using the JMS API to use a queue browser is to just use the
> BrowseableEndpoint API in Camel...
> http://camel.apache.org/browsableendpoint.html
>
> BrowseableEndpoint endpoint =
> camelContext.getEndpoint("activemq:queue:out",
> BrowseableEndpoint.class);
> int messageCount = endpoint.getExchanges().size();
>
>
> 2010/1/4 Ricardo Melo <ri...@cflex.com.br>:
> > Hi,
> >
> > I'm using camel and I need to get the number of elements present in a
> > activemq queue, because the queue has a max size.
> >
> > I'm trying to use QueueBrowser Interface, with no success. The queue I
> put
> > messages in is called "activemq:queue:out".
> >
> > I've found some code to do that job, but I can't connect to the queue, a
> > javax.naming.
> > NoInitialContextException is thrown at the identified line showed bellow.
> > The connection string is not working. What should I use to connect to the
> > queue created by camel?
> >
> > Here is the code:
> >
> >        // get the initial context
> >        InitialContext ctx = new InitialContext();
> >
> >        // lookup the queue object
> >        [EXCEPTION!] Queue queue = (Queue) ctx.lookup("queue:out");
> >
> >        // lookup the queue connection factory
> >        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
> >        lookup("queue/connectionFactory");
> >
> >        // create a queue connection
> >        QueueConnection queueConn = connFactory.createQueueConnection();
> >
> >        // create a queue session
> >        QueueSession queueSession = queueConn.createQueueSession(false,
> >        Session.AUTO_ACKNOWLEDGE);
> >
> >        // create a queue browser
> >        QueueBrowser queueBrowser = queueSession.createBrowser(queue);
> >
> >        // start the connection
> >        queueConn.start();
> >
> > Thanks in advance,
> > Ricardo Melo
> >
>
>
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>



-- 
Ricardo Britto Melo

CFlex - Empower your Decisions
Tel: (+55 19) 3251-5211
Rua Barão de Paranapanema, 401A
Campinas - SP
www.cflex.com.br

Re: Issue trying to browse a activemq queue

Posted by James Strachan <ja...@gmail.com>.
BTW it looks like you are confusing the queue name in JMS with the
JNDI name in JNDI.

Incidentally a simpler alternative to using the JNDI API to lookup the
queue then using the JMS API to use a queue browser is to just use the
BrowseableEndpoint API in Camel...
http://camel.apache.org/browsableendpoint.html

BrowseableEndpoint endpoint =
camelContext.getEndpoint("activemq:queue:out",
BrowseableEndpoint.class);
int messageCount = endpoint.getExchanges().size();


2010/1/4 Ricardo Melo <ri...@cflex.com.br>:
> Hi,
>
> I'm using camel and I need to get the number of elements present in a
> activemq queue, because the queue has a max size.
>
> I'm trying to use QueueBrowser Interface, with no success. The queue I put
> messages in is called "activemq:queue:out".
>
> I've found some code to do that job, but I can't connect to the queue, a
> javax.naming.
> NoInitialContextException is thrown at the identified line showed bellow.
> The connection string is not working. What should I use to connect to the
> queue created by camel?
>
> Here is the code:
>
>        // get the initial context
>        InitialContext ctx = new InitialContext();
>
>        // lookup the queue object
>        [EXCEPTION!] Queue queue = (Queue) ctx.lookup("queue:out");
>
>        // lookup the queue connection factory
>        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
>        lookup("queue/connectionFactory");
>
>        // create a queue connection
>        QueueConnection queueConn = connFactory.createQueueConnection();
>
>        // create a queue session
>        QueueSession queueSession = queueConn.createQueueSession(false,
>        Session.AUTO_ACKNOWLEDGE);
>
>        // create a queue browser
>        QueueBrowser queueBrowser = queueSession.createBrowser(queue);
>
>        // start the connection
>        queueConn.start();
>
> Thanks in advance,
> Ricardo Melo
>



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Issue trying to browse a activemq queue

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

In ActiveMQ 5.3 you can get stats as follows:
http://activemq.apache.org/statisticsplugin.html

That should be faster than the QueueBrowser interface as you have to
walk every message on the queue to know how many messages exists. So
getting the stats some other way is better and faster.

What do you need JNDI for? Do you run your code in some J2ee server or
the likes?


On Mon, Jan 4, 2010 at 4:26 PM, Ricardo Melo <ri...@cflex.com.br> wrote:
> Hi,
>
> I'm using camel and I need to get the number of elements present in a
> activemq queue, because the queue has a max size.
>
> I'm trying to use QueueBrowser Interface, with no success. The queue I put
> messages in is called "activemq:queue:out".
>
> I've found some code to do that job, but I can't connect to the queue, a
> javax.naming.
> NoInitialContextException is thrown at the identified line showed bellow.
> The connection string is not working. What should I use to connect to the
> queue created by camel?
>
> Here is the code:
>
>        // get the initial context
>        InitialContext ctx = new InitialContext();
>
>        // lookup the queue object
>        [EXCEPTION!] Queue queue = (Queue) ctx.lookup("queue:out");
>
>        // lookup the queue connection factory
>        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
>        lookup("queue/connectionFactory");
>
>        // create a queue connection
>        QueueConnection queueConn = connFactory.createQueueConnection();
>
>        // create a queue session
>        QueueSession queueSession = queueConn.createQueueSession(false,
>        Session.AUTO_ACKNOWLEDGE);
>
>        // create a queue browser
>        QueueBrowser queueBrowser = queueSession.createBrowser(queue);
>
>        // start the connection
>        queueConn.start();
>
> Thanks in advance,
> Ricardo Melo
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus