Skip to main content

EJB 3 Timers

15 replies [Last post]
tannic2k7
Offline
Joined: 2009-07-09

I have been trying to get a timer to work for a long time. I keep getting null exceptions on the line when I create the timer. I tried sessioncontext way and just timerservice way but I'm getting no where with it. Can someone help me out?

Here is the code for the bean

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class Timers implements TimerLocal, TimedObject {
@Resource
SessionContext ctx = null;
private TimerService ts;

/**
* Default constructor.
*/

public Timers() {
// TODO Auto-generated constructor stub

}
public void createTimer()
{
long testNum = 3000;
ts.createTimer(testNum, null);
System.out.println("checking");
}

@Timeout
public void timeout(Timer time)
{
System.out.println("Timer Bean is working!!!!");
}
@Override
public void ejbTimeout(Timer time) {
// TODO Auto-generated method stub
System.out.println("Timer Bean is working!!!!");
}
}

client code
package client.session.TimerBean;

import session.TimerBean.*;
public class Client {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Timers Test = new Timers();
Test.createTimer();

}

}

And the interface
package session.TimerBean;
import javax.ejb.Local;

@Local
public interface TimerLocal {
public void createTimer();
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Marina Vatkina

If you create a bean using new, nothing will be injected in that bean.

Regards,
-marina

glassfish@javadesktop.org wrote:
> I have been trying to get a timer to work for a long time. I keep getting null exceptions on the line when I create the timer. I tried sessioncontext way and just timerservice way but I'm getting no where with it. Can someone help me out?
>
> Here is the code for the bean
>
> @Stateless
> @TransactionManagement(TransactionManagementType.BEAN)
> public class Timers implements TimerLocal, TimedObject {
> @Resource
> SessionContext ctx = null;
> private TimerService ts;
>
>
> /**
> * Default constructor.
> */
>
> public Timers() {
> // TODO Auto-generated constructor stub
>
> }
> public void createTimer()
> {
> long testNum = 3000;
> ts.createTimer(testNum, null);
> System.out.println("checking");
> }
>
> @Timeout
> public void timeout(Timer time)
> {
> System.out.println("Timer Bean is working!!!!");
> }
> @Override
> public void ejbTimeout(Timer time) {
> // TODO Auto-generated method stub
> System.out.println("Timer Bean is working!!!!");
> }
> }
>
>
> client code
> package client.session.TimerBean;
>
> import session.TimerBean.*;
> public class Client {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> Timers Test = new Timers();
> Test.createTimer();
>
> }
>
> }
>
> And the interface
> package session.TimerBean;
> import javax.ejb.Local;
>
> @Local
> public interface TimerLocal {
> public void createTimer();
> }
> [Message sent by forum member 'tannic2k7' (tannic2k7)]
>
> http://forums.java.net/jive/thread.jspa?messageID=360810
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

tannic2k7
Offline
Joined: 2009-07-09

can you tell me how that's done?

chrjohn
Offline
Joined: 2008-03-11

Hi,

you need to lookup the bean in the InitialContext. Please see the EJB FAQ: https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

Cheers
Chris.

tannic2k7
Offline
Joined: 2009-07-09

Does the code above works? I'm not running off the server just yet. Trying to run as a java application before I just let it lose on the server.

chrjohn
Offline
Joined: 2008-03-11

If you do not let it run on an application server, what should inject references into the EJB?

tannic2k7
Offline
Joined: 2009-07-09

Sorry but this is my first time programming an EJB and need a timer. I have a clue of what you mean but not a clue how to implement it. I plan on running it on a server. However, the client side is in the bean itself. Meaning that I'm testing the bean and not calling it from a server. I can deploy it on a server. There are no coding errors. However, I should use the initial context link from above and call it from the server like that correct?

chrjohn
Offline
Joined: 2008-03-11

Hi,

I also think that you need to annote the TimerService with @Resource, otherwise the TimerService is not injected. Like this:
@Resource
private TimerService timerService;

When you have deployed the EJB then you can look it up in the IntialContext, right.

Cheers
Chris.

tannic2k7
Offline
Joined: 2009-07-09

ok I'll do that...
However, is there a way you can test the bean without putting in on a server or you have to put it on the server in order to test it?

Message was edited by: tannic2k7

Marina Vatkina

It's coming with EJB 3.1 as an embedded EJB feature. Otherwise you need to look
for specific tools that allow you to do it.

Regards,
-marina

glassfish@javadesktop.org wrote:
> ok I'll do that...
> However, is there a way you can test the bean without putting in on a server or you have to put it on the server in order to test it.
> [Message sent by forum member 'tannic2k7' (tannic2k7)]
>
> http://forums.java.net/jive/thread.jspa?messageID=360971
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

tannic2k7
Offline
Joined: 2009-07-09

I'm using Eclipse...

InitialContext ic = new InitialContext();
______________ = ic.lookup("TimerBean");

I would put an object on the left side of the equal sign correct?

tannic2k7
Offline
Joined: 2009-07-09

Or can you use a regular java application?

tannic2k7
Offline
Joined: 2009-07-09

And would you test this bean on the server. I mean what project would you have to create. i see that you can't just use a java application and go on from there.

tannic2k7
Offline
Joined: 2009-07-09

Well I got it to work... finally.

tannic2k7
Offline
Joined: 2009-07-09

Thanks for the ideas everyone.

chrjohn
Offline
Joined: 2008-03-11

You're welcome, thanks for letting us know the outcome.