What causes the message redelivery?
Hi all,
I got some problems running IMQ3.0 on w2k.
Following is my runtime enviroment.
OS :w2k sp2
IMQ:3.0
JDK:j2sdk1.4
RDBMS :Oracle 816
My problem is that when my queuelistener receives messages from the message broker, it receives the same message serveral times. This isn't the result i expected.
Does this problem occur only in my program, or it is a known bug of IMQ3.0?
The following is my code sample(queuelistener), please help me to fix this problem.
I would appreciate if you could suggest how to overcome this problem.
/////////////////////////////////////////////////////////////////////////////// //////
package aspno1.pshs.backend.listener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import org.apache.log4j.Category;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
public class DemoListener implements Runnable, MessageListener{
Category CAT = Category.getInstance(DemoListener.class);
String _name;
boolean running = true;
Context _ctx = null;
QueueReceiver _receiver = null;
QueueConnectionFactory _factory = null;
QueueConnection _conn = null;
QueueSession _sess = null;
Queue _queue = null;
static int errorCount = 0;
static boolean connect_ok = true;
public DemoListener(String name) {
super();
_name = name;
}
public void run(){
while(running){
Timestamp current = new Timestamp(System.currentTimeMillis());
if(isWorkingTime(current)){
if(_factory == null){
try{
_factory = (QueueConnectionFactory)_ctx.lookup("PSHSConnectionFactory");
_conn = _factory.createQueueConnection();
_sess = _conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
_queue = (Queue)_ctx.lookup(_name);
_receiver = _sess.createReceiver(_queue);
_conn.start();
synchronized(this){
connect_ok = true;
errorCount = 0;
}
}catch(Exception e){
synchronized(this){
connect_ok = false;
}
//clean up first
cleanup();
//retry again after 5 seonds later.
try{
Thread.currentThread().sleep(5000);
}catch(Exception ignored){
}
}
}
//start to receive message
if(_receiver != null){
Message message = null;
try{
//waiting for 5 seconds
message = _receiver.receive(5000);
}catch(Exception e){
synchronized(this){
connect_ok = false;
}
//clean up all resouces
//rebuild the connection
cleanup();
}
if (message != null &&
message instanceof TextMessage) {
onMessage(message);
}
}
}else{
//If current time is not between the working hours.
if(_factory != null)
cleanup();
try{
Thread.currentThread().sleep(5000);
}catch(Exception ignore){}
}
}
if(_factory != null)
cleanup();
}
public void stop(){
running = false;
}
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage txtMsg = null;
try{
txtMsg = (TextMessage)message;
execute(txtMsg.getText());
}catch(Exception mse){
try{
notifyAdm(_name, mse, "Order Content\n"+txtMsg.getText());
}catch(Exception ignore){}
}
}
}
protected void cleanup(){
//remove all resources
try{
if(_receiver != null)
_receiver.close();
_receiver = null;
if(_sess != null)
_sess.close();
_sess = null;
if(_conn != null)
_conn.close();
_conn = null;
_queue = null;
_factory = null;
}catch(Exception e){
}
}
public void execute(String msg)
throws Exception, JMSException, RemoteException{
System.out.println("Message Content-");
System.out.println(msg);
}
protected boolean isWorkingTime(Timestamp currTime){
if(isHoliday(new Date())){
return false;
}
try{
//return true if the incoming time between the working hours.
return true;
}catch(Exception e){
return false;
}
}
private void notifyAdm(String name, Throwable cause, String txt) {
//send notification to WebMaster
}
public boolean isHoliday(Date time) {
//return true if the incoming date
//is holiday define in the database.
return true;
}
public static void main(String[] args){
Thread t = new Thread(new DemoListener("Test"));
t.start();
}
}
/////////////////////////////////////////////////////////////////////////////// //////

