Java Tutorial - Java Scipt : Stateless Session Beans

Java Tutorial - Java Scipt :

Stateless Session Beans

We won’t actually show an entire code example involving a stateless session bean using messaging. A lot of that is similar to the standalone client. Instea we’ll cover the important differences involved in using JMS from the context
of a stateless session bean.

 As with any component managed by a J2EE application server, stateless session beans will get the proper InitialContext without having to provide any additional parameters.

 The other thing to note is that if you are using container-managed transaction, then you should not call commit() or rollback() explicitly. Normally, the bean will automatically commit if it returns without exception and rollback if an exception occurs. Note that this also implies that the parameters passed into the JMS session creation methods are ignored in favor of the J2EE container’s settings. Using container-managed transactions is recommended for most cases.

 There’s an exception to every rule: In the current implementation of JBossMQ an explicit commit is required before the message is sent in a transacted environment.

Normally, you keep a reference to the topic and the topic connection for efficiency. These references are created in ejbCreate() and released in ejbRemove().

Here’s a small snippet from a typical stateless session bean messaging façade:

public void ejbCreate() throws CreateException
// —- Allocate JMS resources —-
InitialContext context = new InitialContext();
TopicConnectionFactory factory = (TopicConnectionFactory)
context.lookup( “ConnectionFactory” );
m_topic = (Topic) context.lookup( “topic/testTopic” );
m_connection = factory.createTopicConnection();
catch( Exception e )
throw new CreateException( e.getMessage() );

That’s about it! There aren’t even any changes to the descriptors.