aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB3.0 CallBack methods - simple doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3.0 CallBack methods - simple doubt" Watch "EJB3.0 CallBack methods - simple doubt" New topic
Author

EJB3.0 CallBack methods - simple doubt

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
Hi all,

I am a newbie to EJB3.0. I am trying to run a EJB3.0 program using CallBack methods.

Here's the code:

When I try to run the client, the program works perfectly, but none of the callback methods getting called. CAn you please let me know what went wrong?

Thanks,
Guru

Interface - HelloCallBack.java
public interface HelloCallBack {

/**
* @return a greeting to the client.
*/
public String helloCallBack();
}


Bean class: HelloCallBackBean.java

import javax.ejb.*;
import javax.ejb.*;

/**
* Demonstration stateless session bean.
*/
@Stateful
@Remote(HelloCallBack.class)
public class HelloCallBackBean implements HelloCallBack {
public String helloCallBack() {
System.out.println("helloCallBack()");
return "Hello, Call Back World!";
}

@Init
public void sayInit()
{
System.out.println("This would be called upon Init");
}

@PostConstruct
public void sayPostConstruct()
{
System.out.println("This would be called upon post construct");
}


@PreDestroy
public void sayPreDestroy()
{
System.out.println("This would be called upon pre destroy");
}

@Remove
public void sayRemove()
{
System.out.println("This would be called upon Remove");
}

@PrePassivate
public void sayPrePassivate()
{
System.out.println("This would be called upon pre passivate");
}

@PostActivate
public void sayPostActivate()
{
System.out.println("This would be called upon post activate");
}
}


The client: HelloCallBackClient.java

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.*;
import javax.naming.*;



public class HelloCallBackClient {

public static void main(String[] args) throws Exception {
/*
* Obtain the JNDI initial context.
*
* The initial context is a starting point for
* connecting to a JNDI tree. We choose our JNDI
* driver, the network location of the server, etc
* by passing in the environment properties.
*/

Properties p = new Properties();

//The JNDI properties you set depend
//on which server you are using.
//These properties are for the Remote Server.
p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
p.put("java.naming.provider.url", "jnp://localhost:1099");
p.put("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");

Context ctx = new InitialContext(p);
/*
* Get a reference to a bean instance, looked up by class name
*/
HelloCallBack helloCallBack = (HelloCallBack) ctx.lookup("HelloCallBack");


/*
* Call the helloCallBack() method on the bean.
* We then print the result to the screen.
*/
System.out.println(helloCallBack.helloCallBack());
}
}



Everything is working, but I would like to see few callback methods getting called.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

Just a guess - you are expecting to see the System.out messages appear on the console from where you are running this standalone client and you are seeing only the "Hello, Call Back World!" message. Even the callback methods are being invoked, all you have to do is check the server.log file present in %JBOSS_HOME%/server/default/log folder. You will find the messages in that file.

P.S. I don't see anything wrong with the code you posted and the only thing i could think of was that you might be looking at the wrong place for the log messages. Let us know if that's not the case.


[My Blog] [JavaRanch Journal]
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
Thanks. Here's some output from server.log file..Even I don't see any messages in the server.log file whatever I have givn in my SOP (System.out.println).

But, I would like to see my ouput on console. If it is not possible, then what's the use of Callback methods?

2007-07-19 23:52:34,905 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - second pass <Thu, 19 Jul 2007 23:52:34>
2007-07-19 23:52:34,905 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] AtomicActionRecoveryModule: Second pass
2007-07-19 23:52:34,905 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_6] - TORecoveryModule - second pass
2007-07-19 23:52:34,905 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.secondpass] Local XARecoveryModule - second pass
2007-07-19 23:54:34,906 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - first pass <Thu, 19 Jul 2007 23:54:34>
2007-07-19 23:54:34,906 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] StatusModule: first pass
2007-07-19 23:54:34,906 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_3] - TORecoveryModule - first pass
2007-07-19 23:54:34,906 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.firstpass] Local XARecoveryModule - first pass
2007-07-19 23:54:44,906 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - second pass <Thu, 19 Jul 2007 23:54:44>
2007-07-19 23:54:44,906 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] AtomicActionRecoveryModule: Second pass
2007-07-19 23:54:44,906 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_6] - TORecoveryModule - second pass
2007-07-19 23:54:44,906 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.secondpass] Local XARecoveryModule - second pass
2007-07-19 23:56:44,907 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - first pass <Thu, 19 Jul 2007 23:56:44>
2007-07-19 23:56:44,907 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] StatusModule: first pass
2007-07-19 23:56:44,907 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_3] - TORecoveryModule - first pass
2007-07-19 23:56:44,907 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.firstpass] Local XARecoveryModule - first pass
2007-07-19 23:56:54,907 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - second pass <Thu, 19 Jul 2007 23:56:54>
2007-07-19 23:56:54,907 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] AtomicActionRecoveryModule: Second pass
2007-07-19 23:56:54,907 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_6] - TORecoveryModule - second pass
2007-07-19 23:56:54,907 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.secondpass] Local XARecoveryModule - second pass
2007-07-19 23:57:15,016 DEBUG [org.jboss.ejb.plugins.LRUEnterpriseContextCachePolicy] Running RemoverTask
2007-07-19 23:57:15,016 DEBUG [org.jboss.ejb.plugins.LRUEnterpriseContextCachePolicy] RemoverTask, PassivatedCount=0
2007-07-19 23:57:15,016 DEBUG [org.jboss.ejb.plugins.AbstractInstanceCache] removePassivated, now=1184869635016, maxLifeAfterPassivation=1200000
2007-07-19 23:57:15,016 DEBUG [org.jboss.ejb.plugins.LRUEnterpriseContextCachePolicy] RemoverTask, done
2007-07-19 23:58:54,908 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - first pass <Thu, 19 Jul 2007 23:58:54>
2007-07-19 23:58:54,908 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] StatusModule: first pass
2007-07-19 23:58:54,908 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_3] - TORecoveryModule - first pass
2007-07-19 23:58:54,908 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.firstpass] Local XARecoveryModule - first pass
2007-07-19 23:59:04,908 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] Periodic recovery - second pass <Thu, 19 Jul 2007 23:59:04>
2007-07-19 23:59:04,908 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] AtomicActionRecoveryModule: Second pass
2007-07-19 23:59:04,908 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_6] - TORecoveryModule - second pass
2007-07-19 23:59:04,908 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.recovery.info.secondpass] Local XARecoveryModule - second pass


Thanks,
Guru
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

But, I would like to see my ouput on console. If it is not possible, then what's the use of Callback methods?


Your standalone java client runs in its own JVM. The bean is deployed on the server and its operations(including the callback methods) are executed on the server. As a result all the SystemOut logs will go to the *server's* console and the server log file.

Could you post the logs when JBoss is starting or deploying your application?
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
Thank you. Exactly, I would like to see all SOP log messages to be printed in JBOSS's console only. I am not getting that in JBOSS's console. Here's some of the JBOSS's console ouput.
...
...
ptor.java:91)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
at $Proxy34.create(Unknown Source)
at org.jboss.deployment.MainDeployer.create(MainDeployer.java:969)
at org.jboss.deployment.MainDeployer.create(MainDeployer.java:959)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:818)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:782)
at sun.reflect.GeneratedMethodAccessor18.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractIntercept
or.java:133)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelM
BeanOperationInterceptor.java:142)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
at $Proxy9.deploy(Unknown Source)
at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymen
tScanner.java:421)
at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentS
canner.java:634)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
doScan(AbstractDeploymentScanner.java:263)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
loop(AbstractDeploymentScanner.java:274)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
run(AbstractDeploymentScanner.java:225)
10:09:40,654 INFO [EJB3Deployer] Deployed: file:/D:/jboss-4.2.0.GA/server/defau
lt/tmp/deploy/tmp32989CallBackStateFul3.jar-contents/CallBackStateFul1.jar
10:09:40,654 INFO [EJB3Deployer] Deployed: file:/D:/jboss-4.2.0.GA/server/defau
lt/tmp/deploy/tmp32989CallBackStateFul3.jar-contents/CallBackStateFul2.jar-conte
nts/CallBackStateFul1.jar
10:09:40,654 INFO [EJB3Deployer] Deployed: file:/D:/jboss-4.2.0.GA/server/defau
lt/tmp/deploy/tmp32989CallBackStateFul3.jar-contents/CallBackStateFul2.jar
10:09:40,654 INFO [JmxKernelAbstraction] creating wrapper delegate for: org.jbo
ss.ejb3.stateful.StatefulContainer
10:09:40,669 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=CallB
ackStateFul3.jar,name=HelloCallBackBean,service=EJB3 with dependencies:
10:09:40,779 INFO [EJBContainer] STARTED EJB: HelloCallBackBean ejbName: HelloC
allBackBean
10:09:41,138 INFO [EJB3Deployer] Deployed: file:/D:/jboss-4.2.0.GA/server/defau
lt/deploy/CallBackStateFul3.jar
10:14:16,921 INFO [STDOUT] helloCallBack()
10:14:26,140 INFO [STDOUT] helloCallBack()

My EJB file is CAllBackStateFul3.jar. Everytime, I call the hello method, it prings the above message "helloCallBack()", but i am not getting any messages from call back methods.

Please help,
Guru
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

Originally posted by Gurumurthy Ramamurthy:
Exactly, I would like to see all SOP log messages to be printed in JBOSS's console only. I am not getting that in JBOSS's console.



Ok then, you are looking at the right place for the logs.

Let's try this - add the following statement to the main method of your client program after the System.out.println(helloCallBack.helloCallBack()) :



Note: Make sure you add the sayRemove method to the HelloCallBack interface.

Then when you run the client, i would expect the following output on the server console:

INFO [STDOUT] helloCallBack()
INFO [STDOUT] This would be called upon Remove
INFO [STDOUT] This would be called upon pre destroy


Let us know if that's what you see.
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
I am not able to compile the client itself.
c:\guru>javac HelloCallBackClient.java
HelloCallBackClient.java:48: cannot find symbol
symbol : method sayRemove()
location: interface HelloCallBack
System.out.println(helloCallBack.sayRemove());
^
1 error

I guess, this is expecting the interface to have the method sayRemove(). But, as per the call methods, we need to define the method only in the bean class.

Thanks,
Guru
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

Originally posted by Gurumurthy Ramamurthy:

I guess, this is expecting the interface to have the method sayRemove().


That's why i posted the Note in my previous post:


Originally posted by Jaikiran Pai:

Note: Make sure you add the sayRemove method to the HelloCallBack interface.


Just to let you know why i am asking you to do all this is, the callback methods that you expecting messages from *need not be called every time* you invoke a method on the bean. Those methods will have already been called (and the messages printed in the logs) when the bean was instantiated.
[ July 19, 2007: Message edited by: Jaikiran Pai ]
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
Hey, come on. This is just like adding another method in interface and implementing that in the bean. That would work.

ok, I have done the same way on another method.
I have declared the method public void sayInit() in interface
I have implemented this method in the bean as follows

@Init
public void sayInit()
{
System.out.println("this would be called upon init");
}

this is definitely a call back method. Upon the initialization of bean, this would be called. This should not be called explicitly from the Bean client.

But this method did not get called...? This is the problem.

Thanks,
Guru
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

Originally posted by Gurumurthy Ramamurthy:
Hey, come on. This is just like adding another method in interface and implementing that in the bean. That would work.

As per the documentation for the @Remove method:

You can always explicitly call the @Remove annotated method to force the container to remove the bean instance. After the method call is completed, the EJB 3.0 container calls the @PreDestroy tagged method, if there is any


See the sentence mark in bold. So, i am expecting the @PreDestroy callback method to be called when you call the sayRemove method.


So if you see the "This would be called upon pre destroy" message, it would mean that the callback method is invoked. And that's what you wanted to test, right?

Originally posted by Gurumurthy Ramamurthy:
ok, I have done the same way on another method.
I have declared the method public void sayInit() in interface
I have implemented this method in the bean as follows

@Init
public void sayInit()
{
System.out.println("this would be called upon init");
}

this is definitely a call back method. Upon the initialization of bean, this would be called. This should not be called explicitly from the Bean client.

But this method did not get called...? This is the problem.



I might be asking a very obvious question, but did you check the entire log file (from the time when JBoss server was started) to see whether this message is present in the logs.
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
ok, i had the sayPreDestroy() method also. But after firing the sayRemove() method, it did not call the sayPreDestroy() method.

So, i have undeployed, restarted jboss and redeployed.

wow... I could see that sayInit() method getting fired. I havent called this explicitly from the client.

Thanks,
Guru
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3.0 CallBack methods - simple doubt