In previous posts I described converting the examples of chapters 2 and 3 of my book from Glassfish to Weblogic 10.3. In this post I describe the conversions of chapters 4 to 10.
The conversions of chapter4 (Object/Relational Mapping), chapter 5 (JPQL), chapter 6 (Entity Manager) and chapter 7 (Transactions) from Glassfish to Weblogic presented no problems at all. However in chapter 8 (Messaging) the creation of JMS resources such as queues and topics is a bit different. JMS resources can be configured using the administrator console or from the command line using the ant wlconfig task. It is also possible to configure JMS resources using the weblogic.Admin utility or the Weblogic scripting tool (WLST). I used the wlconfig task as it was closest to the approach I used with Glassfish. JMS resources are configured using MBeans. An MBean is a Java class which is used for managing, controlling and monitoring a resource. MBeans form a hierarchy with the Weblogic server MBean being the parent of the JMS MBeans we create. First, nested within the wlconfig tag, we use the query tag to get a reference to the examplesServer MBean and store it in the examplesServer property; we will make use of this property shortly.
<query domain="wl_server" type="Server" name="examplesServer" property="examplesServer"/> Weblogic has two preconfigured domains: medrec and wl_server. We choose to use wl_server. Within wl_server is a preconfigured Weblogic server called examplesServer which we will use. A Weblogic server is a Weblogic instance running in its own JVM and with its own configuration. Next we create a JMSServer MBean and its dependent JMSQueue and JMSTopic MBeans using the create tag: <create type="JMSServer" name="BankJMSServer"> <set attribute="Targets" value="${examplesServer}"/> <create type="JMSQueue" name="BankServiceJMSQueue"> <set attribute="JNDIName" value="BankServiceJMSQueue"/> </create> <create type="JMSTopic" name="BankServiceJMSTopic"> <set attribute="JNDIName" value="BankServiceJMSTopic"/> </create> </create> We use the set tag and "Targets" attribute together with the examplesServer property we obtained from the earlier query tag to specify the Weblogic server. Next we create a JMSConnectionFactory as follows: <create type="JMSConnectionFactory" name="BankServiceConnectionFactory"> <set attribute="Targets" value="${examplesServer}"/> <set attribute="JNDIName" value="BankServiceConnectionFactory"/> <set attribute="XAConnectionFactoryEnabled" value="true"/> </create> You don't always need to create a JMSConnectionFactory, you can just use the default JMSConnectionFactory preconfigured with Weblogic. However my existing Glassfish source code refers to the BankServiceConnectionFactory, so I included this step. Chapter 9 (EJB Timer Service) again presented no problems. However in chapter 10 (Interceptors) I did encounter an issue with Interceptor classes. Labs 2 and 3 use the following interceptor class: public class Auditor { @PersistenceContext(unitName="BankService") private EntityManager em; @AroundInvoke public Object audit(InvocationContext invctx) throws Exception {
. . . The interceptor class is referred to in the BankServiceBean as follows:
@Stateless
@Interceptors(Auditor.class)
public class BankServiceBean implements BankService {
@PersistenceContext(unitName="BankService")
private EntityManager em;
...
This presented no problems in Glassfish. With Weblogic I got a runtime error on the lines of
" ... Dependency injection failure: can't find the bean definition about class interface javax.persistence.EntityManager. No unique bean of type javax.persistence.EntityManager is defined ... "
This was resolved by giving a JNDI name to the EntityManager in the BankServiceBean as follows:
@Stateless(mappedName="BankService") @Interceptors(Auditor.class) public class BankServiceBean implements BankService { @PersistenceContext(name = "BankPC", unitName="BankService") private EntityManager em;
...
We then use a JNDI lookup in the interceptor class to obtain the EntityManager as follows:
public class Auditor { @AroundInvoke public Object audit(InvocationContext invctx) throws Exception { try { InitialContext ctx = new InitialContext(); EntityManager em = (EntityManager)ctx.lookup("java:comp/env/BankPC"); ... That's it for now. In my next post I'll deal with the remaining chapters on EJB Web Services and Security respectively. Authentication in particular is somewhat application server specific so I expect difficulties in this area.