Welcome!

Java Authors: Liz McMillan, Walter H. Pinson, III, Maureen O'Gara, Yakov Werde, Tony Bishop

Related Topics: Java

Java: Article

Spring and Java EE 5 (PART 2)

Simplicity and power combined

Now let's explore how the Spring bean factory is created and how the Spring configuration is provided.

When an EJB instance is created (when a client invokes an EJB), the onEjbCreate method is invoked automatically. A JNDI lookup is done to obtain the path for the bean factory by using an environment entry named ejb/BeanFactoryPath. So you have to define it in the EJB deployment descriptor for the EJB:

<session>
<display-name>PlaceBid</display-name>
<ejb-name>PlaceBid</ejb-name>
<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>/actionBazaar-service.xml</env-entry-value>
</env-entry>
</session>

Although EJB 3 made deployment descriptor optional there are a few cases where you still have to use it. If you're a heavy Spring user you live and breath XML configurations so you have nothing to complain about! In our example we've set the env-entry-value for the ejb/BeanFactoryPath environment variable at /actionBazaar-service.xml. So you have to package the EJB classes, Spring classes, and Spring configuration file into your ejb-jar package.

This way you can use the declarative security, transaction, remoteability, and Web Service features of EJB and combine that with POJO injection, AOP, simplified data, and resource access of Spring. Next we'll see a powerful combination of MDB and Spring JmsTemplate.

JmsTemplate and Message-Driven Beans
Message-driven Beans are simple to develop and can be configured as a message consumer. MDBs support several performance features such as pooling and may either be used with a JMS-compliant provider or an EIS using a JCA 1.5-compliant connector. Below is a simple MDB configured against a JMS provider.

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName =
"destinationName", propertyValue =
"jms/OrderBillingQueue"),
@ActivationConfigProperty(propertyName =
"destinationType",propertyValue =
"javax.jms.Queue") })
public class OrderBillingMDB implements MessageListener {
public void onMessage(Message message) {

}

Building JMS producer with Java EE 5 is still a pretty complex task in spite of its injection support. Spring makes building a message producer application very simple as evident in the following code:

public void sendMessage() {
try {
..
JMSSender jmsSender =
(JMSSender)appContext.getBean("jmsSender");
jmsSender.sendMesage();

} catch(Exception e) {
e.printStackTrace();
}
}

Looks pretty simple! You must be wondering: what is the connection factory and destination configured that this client is sending a message to? The magic is done in background by the Spring configuration as specified below:

<bean id="connectionFactory">
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jms/QueueConnectionFactory</value>
</property>
</bean>
<bean id="jmsDestination"
class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">
<value>jms/OrderBillingQueue</value>
</property>
</bean>
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate102">
<property name="connectionFactory">
<ref bean="connectionFactory"/>
</property>
<property name="defaultDestination">
<ref bean="jmsDestination"/>
</property>

</bean>
<bean id="jmsSender" class="oracle.fusion.demo.OrderService">
<property name="jmsTemplate">
<ref bean="jmsTemplate"/>
</property>
</bean>

If you look at this code, the JMS producer code is very simple, however, you have to write quite a bit of XML configuration. It's your choice!

More Stories By Debu Panda

Debu Panda, lead author of the recently published EJB 3 in Action (Manning Publications), is a senior principal product manager on the Oracle Application Server development team, where he drives development of the Java EE container. He has more than 15 years of experience in the IT industry and has published numerous articles on enterprise Java technologies and has presented at many conferences. Debu maintains an active blog on enterprise Java at http://www.debupanda.com.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
jcl 11/24/09 01:16:00 PM EST

Hi,thank you for this tutorial

I'm interested on the first way to intregate Spring and EJB3.

I have tried it in a example project buy it doesn't run. I'm searching since many time a solution,but nothing.

I have posted on Spring forum,but no one seems can help me.

I appreciate if you can help me.Thank you

Antonio