[jboss-user] [EJB 3.0] - Stateful Session Beans Interaction

grdzeli_kaci do-not-reply at jboss.com
Thu Aug 31 03:37:16 EDT 2006


hi all .............
here is my small example :

1.Fierst Remote Interface

  | import javax.ejb.Remote;
  | 
  | @Remote
  | public interface Person {
  | 	public void addPersonName(String a);
  | 	public void printNames();
  | }
  | 

2. And It's Implementation :

  | import java.util.ArrayList;
  | import javax.ejb.Remote;
  | import javax.ejb.Stateful;
  | import javax.ejb.TransactionAttribute;
  | import javax.ejb.TransactionAttributeType;
  | 
  | @Stateful
  | @Remote(Person.class)
  | public class PersonBean implements Person
  | {
  | 	ArrayList<String> list = new ArrayList<String>();
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  | 	public void addPersonName(String name)
  | 	{
  | 		list.add(name);
  | 	}
  | 	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
  | 	public void printNames()
  | 	{
  | 		for (int i = 0; i < list.size(); i++)
  | 		{
  | 			System.out.println("Name = "+list.get(i));	
  | 		}
  | 	}
  | }
  | 

3. Second Remote Interface :

  | import javax.ejb.Remote;
  | @Remote
  | public interface UsePerson {
  | 	public void callPrint()throws Exception;
  | }
  | 

4. And It's Implementation :

  | @Stateful
  | @Remote(Fasade.class)
  | public class UsePersonBean implements UsePerson
  | {
  | 	@EJB 
  | 	Person person;
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  | 	public void callPrint() throws Exception
  | 	{
  | 		try
  | 		{
  | 			test.printNames();
  | 		}
  | 		catch (Exception e)
  | 		{
  | 			e.printStackTrace();
  | 		}
  | 	}
  | 	
  | }
  | 

5. Client code :

  | public class TestClient
  | {
  | 	public static void main(String[] args)
  | 	{
  | 		try
  | 		{
  | 			Properties jndiProps = new Properties();
  | 			jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  | 					"org.jnp.interfaces.NamingContextFactory");
  | 			jndiProps.setProperty(Context.URL_PKG_PREFIXES,
  | 					"org.jboss.naming:org.jnp.interface");
  | 			jndiProps.setProperty(Context.PROVIDER_URL,
  | 					"jnp://127.0.0.1:1099");
  | 			InitialContext ctx = new InitialContext(jndiProps);
  | 
  | 			UsePerson useperson = (UsePerson) ctx.lookup("UsePersonBean/remote");
  | 			Person person = (Person) ctx.lookup("PersonBean/remote");
  | 			person.addPersonName("Piter");
  | 			person.addPersonName("John");
  | 			person.addPersonName("Miles");
  | 			person.addPersonName("Trubambas"); // :)
  | 			bean.callPrint();
  | 		}
  | 		catch (Exception e)
  | 		{
  | 			e.printStackTrace();
  | 		}
  | 	}
  | }
  | 

my task is that i want to get current Person reference in UsePersonBean which created by client.
i think @EJB annotation alwayes makes new reference.
could anybody tell me how i can get current session objects ??
like HTTPSession in Servlets ....
is it right if i use 

  |           private @Resource SessionContext ctx;
  | 
 but somewhere i found that "private @Resource SessionContext ctx;" and  "@EJB annotation" are the same.

thanks.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3968555#3968555

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3968555



More information about the jboss-user mailing list