Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

 This page is under construction.

Basic Operations

Objects

JMS

JMSPool

  • Constructor arguments, refer to the JavaDoc of ThreadPoolExecutor
    • corePoolSize: 10, maximumPoolSize: 20, keepAliveTime: 360, unit: TimeUnit.MILLISECONDS

APIs

receive()

  • for a queue, receive is the standard JMS receive method.
  • for topics, the receive() method is a blocking operation that create a 'temporary subscription'. The 'temporary subscription' wait for the first message and then unsubscribe and return the message. If a 'within' expiry time is provided, the 'temporary subscription' will be unsubscribed when the 'within' time is reached, and return null. This feature is typically used with message selector, and is useful for simulating a synchronous operation with the asynchronous JMS infrastructure. e.g. a client use a new thread to send a request with some delay, and in the main thread listen to the server response topic in synchronous manner.
    Code Block
    def pool = new JMSPool()
    pool.send toQueue:'chatService',message:\[user:'groovy100',room:100\],delay:500 //it sends a message in another thread, the delay is avoid getting a result before the subscribed to the topic
    def result = pool.receive fromTopic:'signedOnBroadcast',messageSelector:"newUser = 'groovy100'"
    if (result) // you have successfully signed on
    else // retry? or tell user to sign in again.
    

...

  • In JMS, Message Selector is a String that defines some filtering conditions for receiving messages. Refer to the JMS Javadoc for details
  • In Groovy Messaging Service API, you may use the messageSelector parameter in receive and onMessage,the message selector parameter takes a String, a List, a Map, or a Closure with one parameter
    • For a String, the String will be used for every destination specified in the same call
    • For a List, it will be matched a against the List of Queue and Topic respectively. If the number of destination is less than the number of message selector items, the excess message selectors will be ignored. If the number of destination is more than the number of message selector items, the excess destination will have no message selector. You could use null for destination or message selector in doing matching. When both fromQueue and fromTopic are used, the two list of destinations will be muched respectively with the same list of message selector.
    • For a Map, the key of the map parameter will be matched against the destination name.
    • For a Closure, it works similar to a map and it passes in the destination name or destination and expect a String of return value as the message selector

 

Advanced Features

JMSPool

Overview

  • JMSPool is an advanced JMS instance that allow multiple threading messaging for incoming and outgoing messages. It is built on Java 5 concurrency library and also utilize the ActiveMQ Pooled Connection Factory. So only Java 5 and AMQ are supported.
  • For simple JMS usage,
    • a new connection and session are created on every JMS execution block. Resources are closed after usage.
    • user has to manage threads and concurrency. Without special coding, messages are sent and received in a single connection and session. 
  • The currently JMSPool is fairly simple. It does not support features like transaction, connection recovery etc. If you need a full feature JMS Pooling product, you are highly recommended to use Spring JMS or Jencks.
  • With JMSPool
    • You could use a JMSPool intance with multiple threads for incoming and outgoing messaging
    • The threads are managed by Java 5 ThreadExecutor
    • Each thread uses a JMS instance

...