On Sun, 2011-01-23 at 06:10 -0800, archana saini wrote:
> Hi All,
>
> I am new to ActiveMQ. I have to write a consumer for activeMQ queue.
> ActiveMQ brokers already setup by other team.
>
> When i am trying to connect to broker using tcp it stuck at line
> connection.start().
> Java control doesnt go beyond this line. I never recieved an
> exception/error.. it seems its waiting for connection.
>
> Belwo is teh simple code i am using. As i havent setup ActiveMQ, i am just
> trying to recieve messge from a queue. I do not have setup info of activeMQ,
> is there some setting required at ActiveMQ setup end.
>
> Note:- ActiveMq setup are on different server i am runnning cosumer on my
> local. So it is connection over teh network.
>
> Code:-
>
> import javax.jms.Connection;
> import javax.jms.ConnectionFactory;
> import javax.jms.Destination;
> import javax.jms.JMSException;
> import javax.jms.MessageConsumer;
> import javax.jms.Session;
>
> import org.apache.activemq.ActiveMQConnectionFactory;
> public class ActiveMQTest {
>
> private static String brokerURL =
> "failover://(tcp://rpc1044.daytonoh.ncr.com:61616?trace=true)";
> private static transient ConnectionFactory factory;
> private transient Connection connection;
> private transient Session session;
>
> private String jobs[] = new String[]{"suspend", "delete"};
>
> public ActiveMQTest() throws JMSException {
> factory = new ActiveMQConnectionFactory(brokerURL);
> System.out.println("------");
> connection = factory.createConnection();
>
> System.out.println("1");
> connection.start();
> System.out.println("2");
> session = connection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
> }
>
> public void close() throws JMSException {
> if (connection != null) {
> connection.close();
> }
> }
>
> public static void main(String[] args) throws JMSException {
> ActiveMQTest consumer = new ActiveMQTest();
> for (String job : consumer.jobs) {
> Destination destination =
> consumer.getSession().createQueue("JOBS." +
> job);
> MessageConsumer messageConsumer =
> consumer.getSession().createConsumer(destination);
> messageConsumer.setMessageListener(new Listener(job));
> }
> }
>
> public Session getSession() {
> return session;
> }
>
>
> }
>
>
>
>
> import javax.jms.Message;
> import javax.jms.MessageListener;
> import javax.jms.ObjectMessage;
>
> public class Listener implements MessageListener {
>
> private String job;
>
> public Listener(String job) {
> this.job = job;
> }
>
> public void onMessage(Message message) {
> try {
> //do something here
> System.out.println(job + " id:" +
> ((ObjectMessage)message).getObject());
> Thread.sleep(1000);
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> }
>
>
> It never prints "2" in logs. it stuck at line connection.start().
>
> Any help would be appreciated. Thanks!
>
>
> Reagrds
> Archana
You are using the failover transport so this kind of behavior is
expected if the broker isn't actually running, or the machine can't make
a connection with the broker for some reason. Try removing the failover
transport and see what kind of error you get from the connection
attempt.
Regards
--
Tim Bish
------------
FuseSource
Email: [email protected]
Web: http://fusesource.com
Twitter: tabish121
Blog: http://timbish.blogspot.com/