0
votes

I am new to activemq. T want to ask a question about the topics of Activemq. I succeed to get a message from a queue. Also I can send message to topic/Queue, but I can't get a message from Topic.

I have tried using Java Code. The result is the same.

The following is my core code:

connection.ClientId = clientId;
connection.Start();
using (ISession session = connection.CreateSession())
{                  
    ITopic topic = new Apache.NMS.Commands.Topic(topicName);
    IDestination destination = SessionUtil.GetDestination(session, topicName, 
                                    DestinationType.Topic);

    using (IMessageConsumer consumer = **session.CreateDurableConsumer**(topic, "news", null, false))
    {
        **consumer.Listener += new MessageListener(consumer_Listener);**     
        //**IMessage iMsg = consumer.Receive();** 
        // if (iMsg != null)//{
        //    ITextMessage msg = (ITextMessage)iMsg;
        //    return msg.Text;         
        // }      
            //else
                //return iMsg;
    }
}

I also using: IMessage iMsg = consumer.Receive(); IMsg always null(topicname has messages. How can I consume topic's message?

2

2 Answers

0
votes

The Messages would need to have been sent after the Topic consumer was created. A Topic is fire and forget, if there are no consumers then the message is discarded. Any consumer that comes online will only receive message sent after that time unless it is either a Durable Topic consumer or a Queue consumer.

In the case of a durable consumer you must have created an instance of it so there is a subscription record before those message were sent to the Topic. So I would guess your problem is that you didn't subscribe this consumer before and so the Broker was not storing any Messages for it.

0
votes

I was so stupid about the phrase "using".Beacause I use "using" open connection and session. when the code block was excuted, the connnection/session is disappear. Now I dont use "using" block to cerate connection. just like normal code. It works. also I build "Global.asax" file. The program can listener Topic once started up. At the same time, I write a function to colse the connection.I tested. Once a message was sent to the topic, the Onessage() function would be exectued.

just resolve my problem.maybe you would have better answer.Thanks Tim.