![]() ![]() ![]() ![]() ![]() ![]() |
This section contains information on the following subjects:
Java Message Service (JMS) can be used in a variety of places in a WebLogic Event Server appliation. In particular:
This section builds on the existing adapter and business logic POJO chapters, so be sure you read them before reading this section:
For general information about JMS, see Java Message Service on the Sun Developer Network.
The section Programming the Adapter Class: Guidelines describes how to generally program an adapter that reads incoming data using the APIs provided by the data feed provider. This section describes additional guidelines you should follow if your adapter reads data from a Java Message Service (JMS) object. Read the general guidelines before you read these JMS guidelines.
For the complete example of how to read JMS data in an adapter, parts of which are described in this section, see the JMS Adapter example located in the WLEVS_HOME
/samples/source/adapters/jms-adapter
directory, where WLEVS_HOME
refers to the main WebLogic Event Server installation, such as /beahome/wlevs20
.
Follow these additional guidelines when programming a JMS adapter:
javax.jms.MessageListener
JMS interface, as well as the com.bea.wlevs.ede.api.Adapter
and com.bea.wlevs.ede.api.EventSource
WebLogic Event Server interfaces:public class InboundJMSAdapter implements Adapter, EventSource, MessageListener, ActivatableBean {...
When you register this adapter class using the <wlevs:adapter>
tag in the EPN assembly file, it will then be referenced by a asyncbean:messgeListener
tag.
The com.bea.wlevs.ede.api.ActivatableBean
interface is optional and allows adapters to react when the dynamic adapter configuration has been set and the event processing network (EPN) of which the adapter is a member is activated. Activation is the last thing that happens during EPN creation
MessageListener.onMessage()
method, adding the code which extracts the event data from the incoming JMS message and puts the data into a WebLogic Event Server event. For example:public void onMessage(Message message) {
List eventCollection = new ArrayList();
try {
Map<String, Object> content = converter.fromMessage(eventType, null, message);
EventType eventType1 = getEventTypeRepository().getEventType(eventType);
EventBuilder eventBuilder = eventType1.getEventBuilderFactory().createBuilder();
for (Map.Entry<String, Object> entry : content.entrySet()) {
eventBuilder.put(entry.getKey(), entry.getValue());
}
Object event = eventBuilder.createEvent();
eventCollection.add(event);
eventSender.sendEvent(eventCollection, null);
} catch (Exception e) {
}
}
In the example, eventType
is an instance property of the adapter which points to the actual event type of the application, jmsEvent
. The content
variable contains the result of converting the incoming JMS message into a WebLogic Event Server event type. The EventBuilder
then builds the WebLogic Event Server event from the message. The EventSender.sendEvent()
method passes the event on to the next component in the network.
See the JMS Adapter example in the product distribution (WLEVS_HOME
/samples/source/adapters/jms-adapter
directory) for the full Java code of the adapter, as well as code for additional classes of the example. In particular, the PassThroughConverter
class parses the incoming javax.jms.Message
, introspects the registered event definition (jmsEvent
), creates events in accordance with the event definition and populates the event property values with data read from the javax.jms.Message
.
This section describes the additional entries you must add to the EPN assembly file when implementing a JMS adapter. See Updating the EPN Assembly File for general information about configuring adapters in the EPN assembly file.
As with any adapter, you register a JMS adapter as usual using the <wlevs:adapter>
tag in the EPN assembly file:
<wlevs:adapter id="inboundJmsAdapter" provider="wl-jms" manageable="true">
<wlevs:instance-property name="eventType" value="jmsEvent" />
</wlevs:adapter>
In the preceding example, provider="wl-jms"
refers to an OSGI-registered adapter factory.
In addition to the standard <wlevs:adapter>
tag, you must add additional entries to the EPN assembly file. In particular, you must configure:
<bean id="wlsjndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
t3://localhost:7001
</prop>
</props>
</property>
</bean>
<bean id="wlsjmsQueueConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="wlsjndiTemplate"/>
</property>
<property name="jndiName">
<value>JMSConnectionQueueFactory01</value>
</property>
</bean>
<asyncbeans:connectionFactory>
tag references the connection factory wlsjmsQueueConnectionFactory
that was created in the entry above, and the <asyncbeans:messageListener>
references the adapter called inboundJmsAdapter, registered with the <wlevs:adapter>
tag:
<asyncbeans:asyncbean id="asyncBean">
<asyncbeans:destinationName>
JMSServer01/com.bea.wlrt.jmsmodule!JMSRequestQueue01
</asyncbeans:destinationName>
<asyncbeans:transactional>false</asyncbeans:transactional>
<asyncbeans:connectionFactory ref="wlsjmsQueueConnectionFactory"/>
<asyncbeans:messageListener ref="inboundJmsAdapter" />
</asyncbeans:asyncbean>
For additional information about configuring WebLogic Event Server AsyncBeans, see Using WebLogic Event Server AsyncBeans.
AsyncBeans provide a WebLogic Event Server alternative to J2EE message driven beans. It uses Spring's Message Driven POJO (MDP) support to allow a Plain Old Java Object (POJO) to act as a message listener on a JMS queue or topic.
In order to use AsyncBeans declaratively, you must add appropriate tags to the EPN assembly file, located in the META-INF/spring
directory of your application bundle. You can configure multiple asynchronous beans per WebLogic Event Server instance.
See the
XSD Schema for the full Schema description of the <asyncbean>
element you can add to the EPN assembly file.
The following sections provide information on how to program and configure common tasks using AsyncBeans:
You can receive messages asynchronously by implementing the javax.jms.MessageListener interface
.
public class MyMessageListener implements MessageListener {
public void onMessage(Message msg) {
System.err.println("RECEIVED: " + msg);
}
}
Use the following tags in your EPN assembly file to connect this listener to a JMS queue:
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<asyncbean>
<destinationName>TEST.FOO</destinationName>
<connectionFactory>connectionFactory</connectionFactory>
<messageListener>messageListener</messageListener>
</asyncbean>
This section provides an example of a simple message-driven POJO:
public class MyPOJO {
public void deliver(String msg) {
System.err.println("RECEIVED: " + msg);
}
}
Note: | This class has no dependencies on JMS, Spring, or any other container code. |
In your EPN assembly file, you must configure a MessageListenerAdapter
that adapts the POJO to the javax.jms.MessageListener
interface. For example:
<bean id="messageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg><bean class="test.MyPOJO" /></constructor-arg>
<property name="defaultListenerMethod" value="deliver"/>
</bean>
<asyncbean>
<destinationName>TEST.FOO</destinationName>
<connectionFactory>connectionFactory</connectionFactory>
<messageListener>messageListener</messageListener>
</asyncbean>
You enable transactions setting the optional transactional
property to true
in your EPN assembly file:
<asyncbean>
<destinationName>TEST.FOO</destinationName>
<transactional>true</transactional>
<connectionFactory>connectionFactory</connectionFactory>
<messageListener>messageListener</messageListener>
</asyncbean>
Use the Spring JNDI lookup mechanism to lookup ConnectionFactorys and destinations from JNDI.
<jee:jndi-lookup id="myConnectionFactory"
jndi-name="my.connection.Factory">
<jee:environment>
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:10001
</jee:environment>
</jee:jndi-lookup>
<jee:jndi-lookup id="myDestination" jndi-name="my.connection.Factory">
<jee:environment>
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:10001
</jee:environment>
</jee:jndi-lookup>
<asyncbean>
<destination>myDestination</destination>
<connectionFactory>myConnectionFactory</connectionFactory>
<messageListener>messageListener</messageListener>
</asyncbean>
When using transactions, the AsyncBean framework performs a blocking receive to get messages from the underlying destination. These messages are then dispatched to the AsyncBean using a WorkManager. The following sections describe two methods to dispatch messages to an AsyncBean:
Retrieve the WorkManager from the OSGi service registry. Use this method when the WorkManager is configured in the config.xml
file that describes your domain.
For example, the AsyncBean configuration object may look like:
<asyncbean>
<destinationName>TEST.FOO</destinationName>
<transactional>true</transactional>
<connectionFactory>connectionFactory</connectionFactory>
<messageListener>messageListener</messageListener>
<workManager>
<osgi:reference interface="commonj.work.WorkManager"
filter="(name=MyWorkManager"/>
</workManager>
</asyncbean>
Use Spring to access the WorkManager.
For example, the AsyncBean configuration object may look like:
<asyncbean>
<destinationName>TEST.FOO</destinationName>
<transactional>true</transactional>
<connectionFactory>connectionFactory</connectionFactory>
| <messageListener>messageListener</messageListener>
<workManager>
<bean class="com.bea.core.workmanager.WorkManagerFactory"
factory-method="findOrCreate">
<constructor-arg value="MyWorkManager"/><!-- name parameter -->
<constructor-arg value="5"/><!-- min threads constraint -->
<constructor-arg value="10"/><!-- max threads constraint -->
</bean>
</workManager>
</asyncbean>
![]() ![]() ![]() |