• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

EJB3.0: Call Back Listener not working.

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EJB 3.0 Call Back methods are working, but the listeners are not working.

ListenerHelloCallBack.java (Interface)
/**
* This is the ListenerHelloCallBack business interface.
*/

public interface ListenerHelloCallBack {

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


Bean class:
import javax.ejb.*;
import javax.ejb.*;

/**
* Demonstration stateless session bean.
*/
@Stateful
@Remote(ListenerHelloCallBack.class)
@CallbackListener(CallBackListener.class)
public class ListenerHelloCallBackBean implements ListenerHelloCallBack {
public String listenerHelloCallBack() {
System.out.println("listenerHelloCallBack()");
return "Hello, Call Back Listener World!";
}
}

Call Back Listener class:

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

public class CallBackListener {

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

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


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

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

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

public void finalize()
{
System.out.println("Finalize method is getting called...");
}
}


Client:

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


/**
* This class is an example of client code which invokes
* methods on a simple, remote stateless session bean.
*/
public class ListenerHelloCallBackClient {

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
*/
ListenerHelloCallBack listenerHelloCallBack = (ListenerHelloCallBack) ctx.lookup("ListenerHelloCallBack");


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

}
}


But, none of the call back methods like @init, @postconstruct are working.

Please help.

Thanks,
Guru
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I believe you can use Interceptors with Stateful Session beans for the callback methods.

Instead of @CallbackListener(CallbackListener.class)

use @Interceptors({CallbackListener.class}) in your Stateful EJB implementation class. You should find Interceptor annotation class in javax.interceptor.InvocationContext package.

You can leave all the other methods in CallbackListener class as they should be called appropriately.

Best Regards,
Shailesh Kini
 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shailesh.

Yes, it is working now after doing @Interceptor.

But, why we should not use @CallbackListener? Can we use it for atleast stateless session beans?

Thanks,
Guru
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!