Java Reference
In-Depth Information
The client then calls the bean's business method twice.
Coding the Publisher Session Bean
The Publisher bean is a stateless session bean that has one business method. The Publisher
bean uses a remote interface rather than a local interface because it is accessed from the
application client.
The remote interface,
clientsessionmdb-ejb/src/java/sb/PublisherRe-
mote.java
, declares a single business method,
publishNews
.
The bean class,
clientsessionmdb-ejb/src/java/sb/Publisher-
Bean.java
, implements the
publishNews
method and its helper method
chooseType
. The bean class also injects
SessionContext
,
ConnectionFact-
ory
, and
Topic
resources and implements
@PostConstruct
and
@PreDestroy
callback methods. The bean class begins as follows:
@Stateless
@Remote({PublisherRemote.class})
public class PublisherBean implements PublisherRemote {
@Resource
private SessionContext sc;
@Resource(lookup = "jms/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(lookup = "jms/Topic")
private Topic topic;
...
The
@PostConstruct
callback method of the bean class,
makeConnection
, creates
the
Connection
used by the bean. The business method
publishNews
creates a
Session
and a
MessageProducer
and publishes the messages.
The
@PreDestroy
callback method,
endConnection
, deallocates the resources that
were allocated by the
@PostConstruct
callback method. In this case, the method
closes the
Connection
.
Coding the Message-Driven Bean:
MessageBean.java
The message-driven bean class,
clientsessionmdb-ejb/src/java/mdb/Mes-
sageBean.java
, is almost identical to the one in
Chapter 11
,
“
A Message-Driven