About Question enthuware.oce-ejbd.v6.2.547 :
Moderator: admin
-
- Posts: 54
- Joined: Tue Apr 19, 2011 10:32 am
- Contact:
About Question enthuware.oce-ejbd.v6.2.547 :
I would add a @Stateless or @Statefull or @Singleton to show that this is an EJB.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
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.
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.
-
- Posts: 33
- Joined: Sat Nov 24, 2012 8:52 am
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
explaination of third option says
how does this work ? any example, how will I get the referecne to queue in this case , when there is no variable ?A variable is required. It would have been valid if it were put before the class definition
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
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:
Paul.
Here is an example given on Page 452 of EJB 3.1 Specification:
HTH,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(); ... } }
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Sat Nov 24, 2012 8:52 am
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
Thanks for clearing this Paul .
-
- Posts: 42
- Joined: Tue Feb 12, 2013 4:35 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
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;
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;
-
- Posts: 42
- Joined: Tue Feb 12, 2013 4:35 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
Writable static fields, in general, are prohibited in the beans.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;
Further, in 16.2.2, it says,
So I am not sure why the tutorial suggests it to make static.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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
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.
-
- Posts: 47
- Joined: Mon Apr 08, 2013 7:55 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
How injection for 1st option will be done?
- It will look up for some "code" in deployment descriptor... the type should be "javax.jms.Queue" and name "myQueue" ?? Is it right ?
Code: Select all
@Resource private Queue myQueue;
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-ejbd.v6.2.547 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 29 guests