JMS Memory Topic Configuration with Dependency Injection (CDI) and JNDI

From Resin 4.0 Wiki

Revision as of 00:00, 28 January 2012 by Ferg (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Email-48.pngGears-48.pngCookbook-48.png

This example shows a CDI/XML configuration for a reasonably-complicated JMS scenario using configured JMS facades to simplify message sending, and using both CDI and JNDI for compatibility.

  • ConnectionFactory - selecting a JMS Connection Factory implementation
  • MemoryTopic - selecting a JMS memory-based topic implementation for sending notifications
  • ee:Named - setting a CDI name for both injection and for EL wiring in the XML
  • resin:Jndi - saving the beans in JNDI
  • MessageSenderResource - a JMS facade for sending messages. Shows wiring beans together in XML.
  • MyMessageSender - application beans are configured in exactly the same fashion.

WEB-INF/resin-web.xml configuration for the topics

The example below shows the configuration for two memory topics ("red" and "blue"), and the corresponding sending facades configured with the topics. You can use the same technique to configure your own facades.


<web-app xmlns="http://caucho.com/ns/resin"
         xmlns:ee="urn:java:ee"
         xmlns:resin="urn:java:com.caucho.resin"
         xmlns:jms="urn:java:com.caucho.jms"
         xmlns:jms-res="urn:java:com.caucho.jms.resource"
         xmlns:my-jms="urn:java:com.my_com.jms">

  <jms:ConnectionFactoryImpl ee:Named="coreConnectionFactory 
                             resin:Jndi="jms/coreConnectionFactory"/>

  <my-jms:MyMessageSender ee:Named="myMessageSender" 
                            resin:Jndi="jms/myMessageSender"/>

  <!--
     - red topic
    -->
  <jms:MemoryTopic ee:Named="redDestination"
                   resin:Jndi="jms/redDestination">
    <topic-name>redDestination</topic-name>
  </jms:MemoryTopic>

  <jms-res:MessageSenderResource ee:Named="redSender"
                                 resin:Jndi="jms/redSender">
    <connection-factory>${coreConnectionFactory}</connection-factory>
    <destination>${redDestination}</destination>
  </jms-res:MessageSenderResource>

  <!--
     - blue topic
    -->
  <jms:MemoryTopic ee:Named="blueDestination"
                   resin:Jndi="jms/blueDestination">
    <topic-name>blueDestination</topic-name>
  </jms:MemoryTopic>

  <jms-res:MessageSenderResource ee:Named="blueSender"
                                 resin:Jndi="jms/blueSender">
    <connection-factory>${coreConnectionFactory}</connection-factory>
    <destination>${blueDestination}</destination>
  </jms-res:MessageSenderResource>
  
</web-app>

CDI @Inject of the Senders

You can use either CDI injection or JNDI to configure the senders. If you're using another framework, like Spring, you can use the JNDI names to communicate the Resin-configured beans with your Spring beans.

For example, you can inject beans directly into a servlet.

import javax.inject.*;
import com.caucho.services.message.*;

public class MyServlet extends HttpServlet {
  @Inject @Named("redSender")
  private MessageSender _redSender;

  @Inject @Named("blueSender")
  private MessageSender _blueSender;

  public void doGet(HttpServletRequest req, HttpServletResponse res)
  {
    _redSender.send(null, req.getParameter("red"));
    _blueSender.send(null, req.getParameter("blue"));
  }
}
Personal tools
TOOLBOX
LANGUAGES