EJB3 sessions

hi all .............

here is my small example :

1.Fierst Remote Interface Code:

import javax.ejb.Remote;

@Remote

public interface Person {

public void addPersonName(String a);

public void printNames();

}

2. And It's Implementation : Code:

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 : Code:

import javax.ejb.Remote;

@Remote

public interface UsePerson {

public void callPrint()throws Exception;

}

4. And It's Implementation : Code:

@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 : 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"); // :)

useperson .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 Code:

private @Resource SessionContext ctx;

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

thanks.

Message was edited by:

Girl

[3120 byte] By [Girla] at [2007-10-3 3:56:02]
# 1

Remote EJB 3.0 references can be passed through Remote EJB 3.0 business interfaces.

If you want to expose the Person that is contained in UsePersonBean, just define a

business method for it and return it from UsePersonBean. E.g.,

@Remote

public interface UsePerson {

public void callPrint()throws Exception;

public Person getPerson();

}

>>i think @EJB annotation alwayes makes new reference.

It depends on the type of the target bean. If the target is a stateful session bean,

then yes, each injection or lookup results in a new bean identity. If the target

is a stateless session bean, all the references to the same ejb and business interface

are equivalent.

>>could anybody tell me how i can get current session objects ?

>>like HTTPSession in Servlets ....

>>is it right if i use Code:

>>private @Resource SessionContext ctx;

SessionContext and MessageDrivenContext can only be injected into

EJB bean classes or interceptor classes. You can also look them up

within utility code running within the ejb invocation. See

http://forum.java.sun.com/thread.jspa?threadID=764595&tstart=0

>>but somewhere i found that "private @Resource SessionContext ctx;"

>>and "@EJB annotation" are the same.

No, they are not the same. @EJB is logically equivalent to an ejb-ref or

ejb-local-ref. It's used to declare that a component has a dependency

on an ejb.SessionContext is an object that allows an ejb to request

information from the ejb container.

ksaksa at 2007-7-14 21:54:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thank you very much.I have done it. I did my targer session bean stateles and now it works.
Girla at 2007-7-14 21:54:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
good, so how about handing ksaks the ten dukes? ;-)
borbjoa at 2007-7-14 21:54:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...