wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Sun JMS example P-to-P droped msg's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Sun JMS example P-to-P droped msg Watch "Sun JMS example P-to-P droped msg New topic
Author

Sun JMS example P-to-P droped msg's

Larry Cha Cha
Greenhorn

Joined: Jun 06, 2003
Posts: 15
Hi. I have modified the SimpleQueueReceiver.java file provided only very slightly(to continue receiving, and to block until a new message arrives) and am having problems with dropped messages.
If I run their SimpleQueueSender with say 5 messages sent to the queue and subsequently run my Receiver (code below) messages all get through the first time that I add a new JmsDestination.
Once I ctrl-c my program, run their sender again, and then run my receiver the first message gets dropped. If I remove the queue and start it up again the problem works. Any ideas? I am a JMS noob so please be gentle
thanks.
-------------
import java.util.*;
import javax.jms.*;
import javax.naming.*;

/**
This class is responsible for continuously receiving JMS messages from
the queue that is passed in as an argument. A messageHandler method
is invoked on each subsequent message that is received.
*/
public class Receiver
{
// Flag used to indicate whether or not the program should continue to
// read messages.
private boolean m_continueReceiving = true;

// Contains the message listeners of this class.
private ArrayList m_messageListeners;

// The active connection for this Point to Point receiver.
private QueueConnection m_queueConnection = null;

// To receive messages from the queue.
private QueueReceiver m_queueReceiver = null;

/**
This method is responsible for setting the receiving flag
(m_continueReceiving to false) such that following the
retrieval of the next message, the program will stop receiving
and terminate.
*/
public void stopReceiving()
{
m_continueReceiving = false;
}

/**
Launch a new Receiver with the name of the queue.
*/
public static void main(String[] args)
{
// Check that a queue name was provided as an argument and exit displaying
// the correct usage if not.
if (args.length != 1)
{
System.out.println("Usage: java " +
"Receiver <queue-name>");

System.exit(1);
}

System.out.println("Queue name is " + args[0]);

// Initialise the receiver, and continuously receive messages
// notifying the listeners on arrival of each one.
Receiver receiver = new Receiver( args[0] );

// Start the Receiver to continuously receive messages.
receiver.startReceiving();
}

/**
The constructor of Receiver is responsible for initialising
the connection to the queue.
*/
public Receiver( String queueName )
{
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueSession queueSession = null;
Queue queue = null;
m_messageListeners = new ArrayList();

// Create a JNDI API InitialContext object if none exists yet.
try
{
jndiContext = new InitialContext();
}
catch (NamingException e)
{
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}

// Look up connection factory and queue. If either does
// not exist, exit.
try
{
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
}
catch (NamingException e)
{
System.out.println("JNDI API lookup failed: " +
e.toString());
System.exit(1);
}

try
{
// Create a connection.
m_queueConnection = queueConnectionFactory.createQueueConnection();

// Create session from connection; false means session is
// not transacted.
queueSession = m_queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

// Create receiver.
m_queueReceiver = queueSession.createReceiver(queue);

}
catch (JMSException e)
{
System.out.println("Exception occurred creating a connection, session, or receiver: " +
e.toString());
}
}

/**
This method is responsible for starting delivery of messages,
and the subsequent retrieval of messages in the queue, notifying
the MessageArrivalListeners after each one.
*/
public void startReceiving()
{
try
{
// Start the delivery of messages.
m_queueConnection.start();

// Receive all text messages from queue unless notified to stop.
while ( m_continueReceiving )
{
// Receive the next message that arrives. The argument of 0 indicates that the
// call will block indefinitely.
Message message = m_queueReceiver.receive(0);

if (message != null) {
if (message instanceof TextMessage) {
TextMessage messageText = (TextMessage) message;
System.out.println("Reading message: " +
messageText.getText());
} else {
System.out.println("In the else.");

}
}
}
}
catch( JMSException e )
{
System.out.println("Exception occurred when receiving a message: " +
e.toString());
}
}

}
------------------------
Larry Cha Cha
Greenhorn

Joined: Jun 06, 2003
Posts: 15
http://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/client.html
Sorry this is the link to the tutorial and code I am referring to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sun JMS example P-to-P droped msg's