You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Bai Shen <na...@cyrrin.com> on 2007/05/02 23:12:14 UTC
Creating simple producer and consumer
Okay, so I used the code on the JNDI page to create a simple producer, and it
seems to work fine. Then I modified it to be a consumer. However, it never
consumes anything. I've even tried setting the messages to be durable,
which I thought would allow me to run the producer, then the consumer.
What am I missing?
--
View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10294868
Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Re: Creating simple producer and consumer
Posted by James Strachan <ja...@gmail.com>.
On 5/4/07, Bai Shen <na...@cyrrin.com> wrote:
>
>
>
> jlim wrote:
> >
> > For topics the consumer must be created first since messages are only
> > delivered to the available consumers at the time the broker receives the
> > message.
> >
> > For durable subscribers you can create a durable subscriber, kill it,
> > run the producer, then start the subscriber again.
> >
> > http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Session.html#createDurableSubscriber(javax.jms.Topic,%20java.lang.String)
> > http://activemq.apache.org/how-does-a-queue-compare-to-a-topic.html
> >
> > Regards,
> > Jonas
> >
>
> How do you kill the consumer so that it stays subscribed? Whenever I stop
> my consumer program, it gets unsubscribed.
Durable subscriber information is stored in the broker for durable
topic subscriptions, so it can keep message the consumer misses while
its closed.
--
James
-------
http://macstrac.blogspot.com/
Re: Creating simple producer and consumer
Posted by Bai Shen <na...@cyrrin.com>.
jlim wrote:
>
> For topics the consumer must be created first since messages are only
> delivered to the available consumers at the time the broker receives the
> message.
>
> For durable subscribers you can create a durable subscriber, kill it,
> run the producer, then start the subscriber again.
>
> http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Session.html#createDurableSubscriber(javax.jms.Topic,%20java.lang.String)
> http://activemq.apache.org/how-does-a-queue-compare-to-a-topic.html
>
> Regards,
> Jonas
>
How do you kill the consumer so that it stays subscribed? Whenever I stop
my consumer program, it gets unsubscribed.
--
View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10321741
Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Re: Creating simple producer and consumer
Posted by Jonas Lim <jl...@exist.com>.
For topics the consumer must be created first since messages are only
delivered to the available consumers at the time the broker receives the
message.
For durable subscribers you can create a durable subscriber, kill it,
run the producer, then start the subscriber again.
http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Session.html#createDurableSubscriber(javax.jms.Topic,%20java.lang.String)
http://activemq.apache.org/how-does-a-queue-compare-to-a-topic.html
Regards,
Jonas
Bai Shen wrote:
>
> Bai Shen wrote:
>
>>
>> James.Strachan wrote:
>>
>>> Try looking at the queues & consumers in the broker
>>> http://activemq.apache.org/how-can-i-monitor-activemq.html
>>>
>>> --
>>> James
>>> -------
>>> http://macstrac.blogspot.com/
>>>
>>>
>>>
>> I can see the topic in the console and the Enqueue Count shows my
>> messages. But I can't seem to receive them for some reason.
>>
>>
>
> Okay, I ended up changing from a Topic to a Queue and was able to send and
> receive my messages asynch. However, I still don't quite understand the
> durable part. I've read
> http://activemq.apache.org/how-do-durable-queues-and-topics-work.html but
> AFAIK, the PERSISTANT flag is 'sposed to be already on. I even set it, and
> I still wasn't able to get things to work.
>
Re: Creating simple producer and consumer
Posted by Bai Shen <na...@cyrrin.com>.
Bai Shen wrote:
>
>
>
> James.Strachan wrote:
>>
>> Try looking at the queues & consumers in the broker
>> http://activemq.apache.org/how-can-i-monitor-activemq.html
>>
>> --
>> James
>> -------
>> http://macstrac.blogspot.com/
>>
>>
>
> I can see the topic in the console and the Enqueue Count shows my
> messages. But I can't seem to receive them for some reason.
>
Okay, I ended up changing from a Topic to a Queue and was able to send and
receive my messages asynch. However, I still don't quite understand the
durable part. I've read
http://activemq.apache.org/how-do-durable-queues-and-topics-work.html but
AFAIK, the PERSISTANT flag is 'sposed to be already on. I even set it, and
I still wasn't able to get things to work.
--
View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10310875
Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Re: Creating simple producer and consumer
Posted by Bai Shen <na...@cyrrin.com>.
James.Strachan wrote:
>
> Try looking at the queues & consumers in the broker
> http://activemq.apache.org/how-can-i-monitor-activemq.html
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
>
I can see the topic in the console and the Enqueue Count shows my messages.
But I can't seem to receive them for some reason.
--
View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10309773
Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Re: Creating simple producer and consumer
Posted by James Strachan <ja...@gmail.com>.
Try looking at the queues & consumers in the broker
http://activemq.apache.org/how-can-i-monitor-activemq.html
On 5/3/07, Bai Shen <na...@cyrrin.com> wrote:
>
>
>
> jlim wrote:
> >
> > Hi,
> >
> > Can you check if you're calling the connection's start method
> > (connection.start()) before starting your consumers.
> >
> > You could also try running the example consumer that is included in the
> > distribution to check if the problem is with your consumer.
> >
> > Regards,
> > Jonas
> >
>
> Tried calling connection.start(). That didn't help. Here's the modified
> consumer code that I created.
>
> 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 javax.jms.TextMessage;
> import javax.jms.Topic;
> import javax.jms.TopicSubscriber;
> import javax.naming.Context;
> import javax.naming.InitialContext;
> import javax.naming.NamingException;
>
> public class SimpleConsumer {
>
> private static final org.apache.commons.logging.Log log =
> org.apache.commons.logging.LogFactory
> .getLog(SimpleConsumer.class);
>
> public static void main(String[] args) {
> Context jndiContext = null;
> ConnectionFactory connectionFactory = null;
> Connection connection = null;
> Session session = null;
> Destination destination = null;
> Topic t = null;
> MessageConsumer consumer = null;
> String destinationName = null;
> final int NUM_MSGS;
>
> if ((args.length < 1) || (args.length > 2)) {
> log.info("Usage: java SimpleConsumer <destination-name>
> [<number-of-messages>]");
> System.exit(1);
> }
> destinationName = args[0];
> log.info("Destination name is " + destinationName);
> if (args.length == 2) {
> NUM_MSGS = (new Integer(args[1])).intValue();
> }
> else {
> NUM_MSGS = 1;
> }
>
> try {
> jndiContext = new InitialContext();
> }
> catch (NamingException e) {
> log.info("Could not create JNDI API context: " + e.toString());
> System.exit(1);
> }
>
> try {
> connectionFactory = (ConnectionFactory)
> jndiContext.lookup("ConnectionFactory");
> destination = (Destination) jndiContext.lookup(destinationName);
> t = (Topic) jndiContext.lookup(destinationName);
> }
> catch (NamingException e) {
> log.info("JNDI API lookup failed: " + e);
> System.exit(1);
> }
>
> try {
> connection = connectionFactory.createConnection();
> connection.start();
> session = connection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
> consumer = session.createConsumer(destination);
> TextMessage message = null;
> for (int i = 0; i < NUM_MSGS; i++) {
> message = (TextMessage)consumer.receive(10000);
> log.info("Receiving message: " + message.getText());
> }
> }
> catch (JMSException e) {
> log.info("Exception occurred: " + e);
> }
> finally {
> if (connection != null) {
> try {
> connection.close();
> }
> catch (JMSException e) {
> }
> }
> }
> }
> }
> --
> View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10303400
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
>
--
James
-------
http://macstrac.blogspot.com/
Re: Creating simple producer and consumer
Posted by Bai Shen <na...@cyrrin.com>.
jlim wrote:
>
> Hi,
>
> Can you check if you're calling the connection's start method
> (connection.start()) before starting your consumers.
>
> You could also try running the example consumer that is included in the
> distribution to check if the problem is with your consumer.
>
> Regards,
> Jonas
>
Tried calling connection.start(). That didn't help. Here's the modified
consumer code that I created.
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 javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class SimpleConsumer {
private static final org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory
.getLog(SimpleConsumer.class);
public static void main(String[] args) {
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
Topic t = null;
MessageConsumer consumer = null;
String destinationName = null;
final int NUM_MSGS;
if ((args.length < 1) || (args.length > 2)) {
log.info("Usage: java SimpleConsumer <destination-name>
[<number-of-messages>]");
System.exit(1);
}
destinationName = args[0];
log.info("Destination name is " + destinationName);
if (args.length == 2) {
NUM_MSGS = (new Integer(args[1])).intValue();
}
else {
NUM_MSGS = 1;
}
try {
jndiContext = new InitialContext();
}
catch (NamingException e) {
log.info("Could not create JNDI API context: " + e.toString());
System.exit(1);
}
try {
connectionFactory = (ConnectionFactory)
jndiContext.lookup("ConnectionFactory");
destination = (Destination) jndiContext.lookup(destinationName);
t = (Topic) jndiContext.lookup(destinationName);
}
catch (NamingException e) {
log.info("JNDI API lookup failed: " + e);
System.exit(1);
}
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(destination);
TextMessage message = null;
for (int i = 0; i < NUM_MSGS; i++) {
message = (TextMessage)consumer.receive(10000);
log.info("Receiving message: " + message.getText());
}
}
catch (JMSException e) {
log.info("Exception occurred: " + e);
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (JMSException e) {
}
}
}
}
}
--
View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10303400
Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Re: Creating simple producer and consumer
Posted by Jonas Lim <jl...@exist.com>.
Hi,
Can you check if you're calling the connection's start method
(connection.start()) before starting your consumers.
You could also try running the example consumer that is included in the
distribution to check if the problem is with your consumer.
Regards,
Jonas
Bai Shen wrote:
> Okay, so I used the code on the JNDI page to create a simple producer, and it
> seems to work fine. Then I modified it to be a consumer. However, it never
> consumes anything. I've even tried setting the messages to be durable,
> which I thought would allow me to run the producer, then the consumer.
>
> What am I missing?
>