Bugs item #1050855, was opened at 2004-10-20 09:16
Message generated for change (Comment added) made by starksm
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=376685&aid=1050855&group_id=22866

>Category: JBossServer
>Group: v4.0
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Mike Engelhart (eztrip)
>Assigned to: Scott M Stark (starksm)
Summary: TimerService exception in MDB

Initial Comment:
I have an MDB that I'm trying to have implement TimedObject but 
when the bean instance is created during ejbCreate() which tries to 
set the timer I'm getting this exception message: 

Cannot obtain inMethodFlag for: getTimerService 

I also get this message during ejbRemove which calls the 
cancelTimer() method.   

I'm using JBoss 4.0.0 final on Mac OS X 10.3.5, Java 1.4.2_05, 2GB 
RAM.

Below is the required test cases.    THere are 3 classes.  
Main.class, MDBBugTest.class and MDBBugExample.class which is 
the actual Message-Driven bean.   The code runs on a stock out of 
the box JBoss 4.0.0 system using the supplied JMS queues with the 
names "A" and "B" that are configured with the default server. 
I used Eclipse 3.0 to build this using the JBoss-IDE and it should 
only rely on the supplied J2EE libraries. 

/* Main stub */

public class Main {

    public static void main(String[] args) throws Exception {
                MDBBugTest test = new MDBBugTest();
                String transactionToken = test.sendTestMessage();
                if (! transactionToken.equals("ERROR")) {
                        test.retrieveMessage(transactionToken);
                }
        }
        
}


/* MDBBugTest stub - generates sample JMS Message and 
retrieves response */
import java.util.Properties;

import javax.jms.Message;
import javax.jms.DeliveryMode;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

public class MDBBugTest {

        public MDBBugTest() {
                super();
                Properties properties = new Properties();
                properties.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.jnp.interfaces.NamingContextFactory");
                properties.put(Context.URL_PKG_PREFIXES, 
"org.jnp.interfaces");
                properties.put(Context.PROVIDER_URL, "localhost");
                try {
                        ctx = new InitialContext(properties);
                        qcf = (QueueConnectionFactory) 
ctx.lookup("UIL2ConnectionFactory");
                        requestQueue = (Queue) ctx.lookup("queue/A");
                        responseQueue = (Queue) ctx.lookup("queue/B");
                } catch (Exception e) {
                        e.printStackTrace();
                }       
        }

        public String sendTestMessage() {
                QueueConnection qc = null;
                QueueSession qs = null;
                try
                {               
                        qc = qcf.createQueueConnection();
                        qc.start();     
                        qs = qc.createQueueSession(false, 
Session.AUTO_ACKNOWLEDGE);                      
                        QueueSender sender = 
qs.createSender(requestQueue);          
                        TextMessage message = qs.createTextMessage();
                        String transactionToken = 
String.valueOf(System.currentTimeMillis());
                        message.setText("SENDING TEST MESSAGE: " + 
transactionToken);
                        message.setStringProperty("UNIQUE_ID", 
transactionToken);
                        sender.send(message, 
DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, 
180000);
                        System.out.println("Message Sent.");    
                        return transactionToken;
                } catch (Exception e) {
                        e.printStackTrace();    
                        return "ERROR";
                } finally {
                        try {
                                qc.close();
                                System.out.println("QueueConnection closed.");
                                qs.close();
                                System.out.println("QueueSession closed.");
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }                       
        }
        
        public void retrieveMessage(String transactionToken) {
                QueueConnection qc = null;
                QueueSession qs = null;
                try {
                        qc = qcf.createQueueConnection();
                        qc.start();
                        qs = qc.createQueueSession(false, 
Session.AUTO_ACKNOWLEDGE);
                        String selector = "UNIQUE_ID = '" + transactionToken 
+ "'";
                        QueueSender sender = 
qs.createSender(responseQueue);
                        MessageConsumer consumer = 
qs.createConsumer(responseQueue,selector);
                        TextMessage received = (TextMessage) 
consumer.receive();
                        System.out.println(received.getText());
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        private InitialContext ctx;
        private QueueConnectionFactory qcf;
        private Queue requestQueue;
        private Queue responseQueue;    
}

/*  
Actual Message-Driven Bean 
Currently the initTimer() method only prints out the error message 
- you may want to have it output the stack trace for more details.   
*/


import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.Timer;
import javax.ejb.TimerHandle;
import javax.ejb.TimerService;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.QueueConnectionFactory;
import javax.jms.TextMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * 
 * @author Mike Engelhart - [EMAIL PROTECTED]
 *
 */

/**
 * @ejb.bean name="MDBBugExample"
 *           display-name="Name for MDBBugExample"
 *           description="MDBBugExample Description"
 *           destination-type="javax.jms.Queue"
 *           acknowledge-mode="Auto-acknowledge"
 * 
 * @jboss.destination-jndi-name
 *  name="queue/A"
 */
public class MDBBugExample implements MessageDrivenBean, 
MessageListener {


        public MDBBugExample() {
                super();
                // TODO Auto-generated constructor stub
        }


        public void setMessageDrivenContext(MessageDrivenContext 
ctx)
                        throws EJBException {
                // TODO Auto-generated method stub
                messageContext = ctx;
        }


        public void ejbRemove() throws EJBException {
                // TODO Auto-generated method stub
                try {
                        qc.close();
                        System.out.println("QueueConnection is closed.");
                
                        cancelTimer();
                        System.out.println("Timer successfully cancelled");
                } catch (JMSException e) {
                        throw new EJBException(e);
                }
        }


        public void onMessage(Message message) {
                QueueSession qs = null;
                try {
                        System.out.println("MDBBugExample: onMessage() 
called");
                        TextMessage msg = (TextMessage) message;
                        System.out.println(msg.getText());
                        String transactionToken = 
msg.getStringProperty("UNIQUE_ID");                     
                        System.out.println(transactionToken);
                        qs = qc.createQueueSession(false, 
Session.AUTO_ACKNOWLEDGE);
                        Queue responseQueue = (Queue) ctx.lookup("queue/
B");
                        QueueSender sender = 
qs.createSender(responseQueue);
                        TextMessage reply = qs.createTextMessage();
                        reply.setText("REPLYING TO MESSAGE: " + 
transactionToken);
                        reply.setStringProperty("UNIQUE_ID", 
transactionToken);
                        sender.send(reply, DeliveryMode.NON_PERSISTENT, 
Message.DEFAULT_PRIORITY, 180000);
                        System.out.println("Message sent");
                } catch (javax.jms.JMSException e) {
                        //      do something
                        System.out.println("got here: JMSException");
                        System.out.println(e.getMessage());

                } catch (NamingException e) {
                        //do something here too
                        System.out.println("got here: NamingException");
                        System.out.println(e.getMessage());
                } finally {     
                        try { 
                                qs.close(); 
                                System.out.println("JBossMQ QueueSession 
Closed");
                        } catch (javax.jms.JMSException e) {
                                System.out.println("nested 
javax.jms.JMSException");
                                e.printStackTrace();
                        }
                }

        }

        public void ejbCreate() {
                try {
                        Properties properties = new Properties();
                        properties.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.jnp.interfaces.NamingContextFactory");
                        properties.put(Context.URL_PKG_PREFIXES, 
"org.jnp.interfaces");
                        properties.put(Context.PROVIDER_URL, "localhost");
                        ctx = new InitialContext(properties);   
                        
                        Queue requestQueue = (Queue) ctx.lookup("queue/
testQueue");         
                        QueueConnectionFactory qcf = 
(QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");
                        qc = qcf.createQueueConnection();       
        

                        initTimer();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                
        }       
        public void initTimer() {
                Date startDate = new Date();
                try {
                        String timerName = "TEST_TIMER";
                        // Create Your Timer
                        TimerService ts = messageContext.getTimerService();
                        Timer timer = ts.createTimer(startDate, 
timerTimeout, timerName);
                        System.out.println("Timer created at " + startDate + " 
with a timeout: " + timerTimeout + " and with info: " + 
timerName);                     
                        // Store the TimerHandle, which is seriablizable 
                        // and which can be used
                        // to retrieve the timer values whenever required 
later.
                        // Class-level attribute:
                        timerHandle = timer.getHandle();
                } catch (Exception e) {
                        System.out.println(e.getMessage());     
                }
                return; 
                
        }
        
        public void cancelTimer() {
                try {
                        String timerName = "TEST_TIMER";
                        TimerService ts = messageContext.getTimerService();
                        Collection timers = ts.getTimers();
                        Iterator it = timers.iterator();
                        while (it.hasNext()) {
                                Timer timer = (Timer) it.next();
                                if ((timer.getInfo().equals(timerName))) {
                                        timer.cancel();
                                        System.out.println("Successfully Cancelled " + 
timerName);                                     
                                }
                        }
                }
                catch (Exception e) {                   
                        System.out.println(e.getMessage());
                }
                return; 
        }       
        
        private MessageDrivenContext messageContext = null;
        private QueueConnection qc = null;
        private InitialContext ctx = null;
        private TimerHandle timerHandle = null;
        private int MAX_NUMBER_OF_SESSIONS = 1;
        private long timerTimeout = 15000;

}



----------------------------------------------------------------------

>Comment By: Scott M Stark (starksm)
Date: 2004-10-21 20:03

Message:
Logged In: YES 
user_id=175228

Although you should be able to get the getTimerService
method from within ejbCreate, you cannot create any timers
from within ejbCreate. I have fixed the problem causing the
'Cannot obtain inMethodFlag for: getTimerService' error, but
the sample mdb you have provided is invalid due to the
subsequent attempt to create timers from within ejbCreate.
Likewise, its not allowed to use the TimerService or Timer
methods from ejbRemove.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=376685&aid=1050855&group_id=22866


-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
JBoss-Development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to