About Question enthuware.oce-ejbd.v6.2.547 :

Moderator: admin

Post Reply
deadlock_gr
Posts: 54
Joined: Tue Apr 19, 2011 10:32 am
Contact:

About Question enthuware.oce-ejbd.v6.2.547 :

Post by deadlock_gr »

I would add a @Stateless or @Statefull or @Singleton to show that this is an EJB.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by admin »

Hi,

1. The question clearly specifies that it is a session bean.
2. The code starts with :
//... valid code
public class TellerBean implements
3. It does not have to be a particular type of session bean.

So I think it is ok.

-Paul.
If you like our products and services, please help us by posting your review here.

rkbansal83
Posts: 33
Joined: Sat Nov 24, 2012 8:52 am
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by rkbansal83 »

explaination of third option says
A variable is required. It would have been valid if it were put before the class definition
how does this work ? any example, how will I get the referecne to queue in this case , when there is no variable ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by admin »

If you put it on the class, the injection will not be done on a variable. However, the code can do lookup using the name given in the annotation.
Here is an example given on Page 452 of EJB 3.1 Specification:
The following code sample illustrates obtaining a JDBC connection when the EJBContext lookupmethod is used.

Code: Select all

@Resource(name=”jdbc/EmployeeAppDB”, type=javax.sql.DataSource)
@Stateless public class EmployeeServiceBean implements EmployeeService {
   @Resource SessionContext ctx;
   public void changePhoneNumber(...) {
     ...
     // use context lookup to obtain resource manager
     // connection factory
     javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/EmployeeAppDB");
     // Invoke factory to obtain a connection. The security
     // principal is not given, and therefore
     // it will be configured by the Deployer.
     java.sql.Connection con = ds.getConnection();
     ...
   }
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

rkbansal83
Posts: 33
Joined: Sat Nov 24, 2012 8:52 am
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by rkbansal83 »

Thanks for clearing this Paul .

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by alex »

Hi Paul,
Can you please elaborate option 2.
@Resource private static Queue myQueue;
Target of an injection cannot be static or final.

I get following from tutorial java ee 7.
Normally, you use the @Resourceannotation to inject a ConnectionFactory, Queue, or
Topicinto your Java EE application. These objects must be created administratively
before you deploy your application. You may want to use the default connection
factory, whose JNDI name is java:comp/DefaultJMSConnectionFactory.
When you use resource injection in an application client component, you normally
declare the JMS resource static:


@Resource(lookup = "java:comp/DefaultJMSConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(lookup = "jms/MyQueue")
private static Queue queue;

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by alex »

Now it is clear,
I opened java ee 6 tutorial.

Using @Resource Annotations in Enterprise Bean or Web Components
When you use the @Resource annotation in an application client component, you normally declare the JMS resource static:

@Resource(lookup = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;

@Resource(lookup = "jms/Queue")
private static Queue queue;
However, when you use this annotation in a session bean, a message-driven bean, or a web component, do not declare the resource static:

@Resource(lookup = "jms/ConnectionFactory")
private ConnectionFactory connectionFactory;

@Resource(lookup = "jms/Topic")
private Topic topic;

If you declare the resource static in these components, runtime errors will result.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by admin »

alex wrote:Hi Paul,
Can you please elaborate option 2.
@Resource private static Queue myQueue;
Target of an injection cannot be static or final.

I get following from tutorial java ee 7.
Normally, you use the @Resourceannotation to inject a ConnectionFactory, Queue, or
Topicinto your Java EE application. These objects must be created administratively
before you deploy your application. You may want to use the default connection
factory, whose JNDI name is java:comp/DefaultJMSConnectionFactory.
When you use resource injection in an application client component, you normally
declare the JMS resource static:


@Resource(lookup = "java:comp/DefaultJMSConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(lookup = "jms/MyQueue")
private static Queue queue;
Writable static fields, in general, are prohibited in the beans.

Further, in 16.2.2, it says,
A field or method of a bean class may be annotated to request that an entry from the bean’s environment be injected. Any of the types of resources or other environment entries described in this chapter may be injected. Injection may also be requested using entries in the deployment descriptor corresponding to each of these resource types. The field or method may have any access qualifier (public, private, etc.) but must not be static.
So I am not sure why the tutorial suggests it to make static.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by admin »

OK, it is talking about the client code not the bean code.
If you like our products and services, please help us by posting your review here.

renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by renatumb »

How injection for 1st option will be done?

Code: Select all

@Resource private Queue myQueue; 
- It will look up for some "code" in deployment descriptor... the type should be "javax.jms.Queue" and name "myQueue" ?? Is it right ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.547 :

Post by admin »

No, as the comment to option 3 explains, "If the name is not specified, a default name is assumed which is generated using the complete class name prefixed to the field name i.e. com.enthu.ejbplus.TellerBean/myQueue"

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests