站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JBoss Seam 1.0.1 正式版英文参考手册

Chapter 6. Messaging in Seam - JBoss Seam 1.0.1 正式版英文参考手册

Chapter 6. Messaging in Seam

Seam makes it easy to send and receive JMS messages to and from Seam components.

6.1. Configuration

To configure Seam's infrastructure for sending JMS messages, you need to tell Seam about any topics and queues you want to send messages to, and also tell Seam where to find the QueueConnectionFactory and/or TopicConnectionFactory.

Seam defaults to using UIL2ConnectionFactory which is the usual connection factory for use with JBossMQ. If you are using some other JMS provider, you need to set one or both of queueConnection.queueConnectionFactoryJndiName and topicConnection.topicConnectionFactoryJndiName in seam.properties, web.xml or components.xml.

You also need to list topics and queues in components.xml to install a Seam managed TopicPublishers and QueueSenders:

<component name="stockTickerPublisher"
          class="org.jboss.seam.jms.ManagedTopicPublisher">
    <property name="topicJndiName">topic/stockTickerTopic</property>
</component>

<component name="paymentQueueSender"
          class="org.jboss.seam.jms.ManagedQueueSender">
    <property name="queueJndiName">queue/paymentQueue</property>
</component>

6.2. Sending messages

Now, you can inject a JMS TopicPublisher and TopicSession into any component:

@In(create=true)
private transient TopicPublisher stockTickerPublisher;   
@In(create=true)
private transient TopicSession topicSession;

public void publish(StockPrice price) {
      try
      {
         topicPublisher.publish( topicSession.createObjectMessage(price) );
      } 
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      } 
}

Or, for working with a queue:

@In(create=true)
private transient QueueSender paymentQueueSender;   
@In(create=true)
private transient QueueSession queueSession;

public void publish(Payment payment) {
      try
      {
         paymentQueueSender.publish( queueSession.createObjectMessage(payment) );
      } 
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      } 
}

6.3. Receiving messages using a message-driven bean

You can process messages using any EJB3 message driven bean. Message-driven beans may even be Seam components, in which case it is possible to inject other event and application scoped Seam components.

6.4. Receiving messages in the client

Seam Remoting lets you subscribe to a JMS topic from client-side JavaScript. This is described in the next chapter.