Stateless Session Bean has no Context when called from WAR

Hello,

Here is the code for my stateless session bean (it will carry out certain actions based on the timer service)

package DigestTest;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.util.Date;

import javax.ejb.*;

import javax.ejb.TimedObject;

import javax.ejb.Timer;

/**

* This is the bean class for the MasterTimerBean enterprise bean.

* Created Sep 7, 2006 9:05:09 AM

* @author ttaylor

*/

public class MasterTimerBean implements SessionBean, MasterTimerRemoteBusiness, MasterTimerLocalBusiness, TimedObject

{

private SessionContext context;

public void setSessionContext(SessionContext aContext)

{

context = aContext;

}

/**

* @see javax.ejb.SessionBean#ejbActivate()

*/

public void ejbActivate()

{

}

/**

* @see javax.ejb.SessionBean#ejbPassivate()

*/

public void ejbPassivate()

{

}

/**

* @see javax.ejb.SessionBean#ejbRemove()

*/

public void ejbRemove()

{

}

// </editor-fold>

/**

* See section 7.10.3 of the EJB 2.0 specification

* See section 7.11.3 of the EJB 2.1 specification

*/

public void ejbCreate()

{

// TODO implement ejbCreate if necessary, acquire resources

// This method has access to the JNDI context so resource aquisition

// spanning all methods can be performed here such as home interfaces

// and data sources.

}

public void ejbTimeout(Timer timer)

{

try

{

BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\timerTest.txt"));

writer.write("Timer has tripped");

writer.newLine();

writer.flush();

writer.close();

}

catch(Exception e)

{

}

return;

}

// Add business logic below. (Right-click in editor and choose

// "EJB Methods > Add Business Method" or "Web Service > Add Operation")

public String initializeTimer()

{

String result = "\n\nInitializing Timer\n";

try

{

// Create Your Timer

if(context!=null)

{

TimerService ts = context.getTimerService();

result = result+"Instantiating Timer Object\n";

Timer timer = ts.createTimer(new Date(), 5000, "TestTimer");

result = result+"About to grab TimerHandle";

TimerHandle timerHandle = timer.getHandle();

}

else

{

result = result+"\nContext Object is null\n";

}

}

catch (Exception e)

{

result = result+"\nError:\n"+e.toString();

}

return result;

}

public String writerTest()

{

String result = "about to write";

try

{

result = result+"\nOpening the file";

BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\timerTest.txt"));

result = result+"\nWriter Test Passed";

writer.write("Writer Works");

writer.newLine();

result = result+"\nFlushing writer";

writer.flush();

result = result+"\nClosing writer";

writer.close();

}

catch (Exception e)

{

result = result+"\n"+e.toString();

}

return result;

}

}

I am calling initializeTimer() from a jsp page with a simple <jsp:useBean tag>.

The program keeps returning that the context variable is not set(this is from the context!=null conditional in initializeTimer()). I am very confused, I thought that setContext(...) was called by the container upon instantiation (and thus should happen completely without any action from me).

How am I able to call a session bean that has not had the context set?

Any information is appreciated,

Thanks,

Scott

[3891 byte] By [FrustratedAndLearning] at [2007-11-14]