org.sandev.tools.util
Class JMSMessager

java.lang.Object
  extended by org.sandev.tools.util.DirectCallMessager
      extended by org.sandev.tools.util.JMSMessager
All Implemented Interfaces:
javax.jms.MessageListener, org.sandev.basics.nodecommon.Messager

public class JMSMessager
extends DirectCallMessager
implements javax.jms.MessageListener

A Messager implementation for use over JMS. This is tested using OpenJMS. Extends DirectCallMessager to provide optimized messaging for nodes on the same server.


Field Summary
protected  org.sandev.basics.nodecommon.CharSerializer charSerializer
          The CharSerializer we use for converting objects to text and back.
protected  javax.jms.TopicConnection connection
          Single connection to the provider is all we need
protected  javax.naming.Context context
           
protected  javax.jms.TopicSession session
          Use a single TopicSession since we're not using transactions
protected  java.util.Hashtable subscriberMap
          Map of topic name to TopicSubscribers
 
Fields inherited from class org.sandev.tools.util.DirectCallMessager
ourInstance, ourNode, subTable
 
Fields inherited from interface org.sandev.basics.nodecommon.Messager
MODE_DIRECT, MODE_INVALID, MODE_SECURE, OPTIMIZE_IF_POSSIBLE, OPTIMIZE_NEVER, WIRE_BASICSECURITY, WIRE_SECURECOMMS, WIRE_TRANSPORTONLY
 
Constructor Summary
JMSMessager()
          Default ctor does nothing.
 
Method Summary
protected  javax.jms.TopicSession createSession()
          Create a JMS TopicSession.
protected  javax.jms.TopicConnection createTopicConnection()
          Create and start a JMS TopicConnection.
protected  org.sandev.basics.structs.SandMessage deserializeJMSMessage(javax.jms.TextMessage message)
          Deserialize a SandMessage serialized as a JMS TextMessage.
protected  void doDeliver(javax.jms.Message message)
          Deserialize a JMS message and pass it to the node's deliver method.
protected  org.sandev.basics.structs.SandMessage doQuery(org.sandev.basics.structs.SandMessage msg, java.lang.String topicName)
          Perform a synchronous query on the given Topic.
protected  org.sandev.basics.structs.SandMessage doQuery(org.sandev.basics.structs.SandMessage msg, java.lang.String recipient, int optimize)
          Query the specified node.
protected  void doReceive(javax.jms.Message message)
          Deserialize a JMS message, pass it to the node's receive method, and return the response to the Topic specified in the JMS message's replyTo field.
protected  void doSend(org.sandev.basics.structs.SandMessage msg, int optimize)
          Send the message.
protected  void doSend(org.sandev.basics.structs.SandMessage msg, java.lang.String topicName)
          Send a SandMessage to the specified JMS topic.
protected  void doSubscribe(org.sandev.basics.nodecommon.Messageable node, java.lang.String msgClass, java.lang.String sender, int optimize)
          Make the subscription happen.
protected  void doUnsubscribe(org.sandev.basics.nodecommon.Messageable node, java.lang.String msgClass, java.lang.String sender, int optimize)
          Make the subscription go away.
 org.sandev.basics.nodecommon.CharSerializer getCharSerializer()
          accessor for charSerializer
protected  javax.naming.Context getContext()
          Create a JNDI Context for JMS Topic lookups.
protected  int getImplementationWireSecurity()
          JMS doesn't specify wire-level security (although some providers support secure transports like SSL).
protected  javax.jms.Topic getTopic(java.lang.String topic)
          Lookup the JMS Topic with the given topic name.
 void init(org.sandev.basics.nodecommon.Messageable node, org.sandev.basics.structs.NodeInstance instance)
           
protected  void jmsSubscribe(java.lang.String topicName)
          Subscribe this messager to a JMS topic.
protected  void jmsUnsubscribe(java.lang.String topicName)
          Unsubscribe this messager from a JMS topic.
 void onMessage(javax.jms.Message message)
          Receive a JMS Message, unpack it, and pass it to the node.
protected  java.lang.String serializeSandMessage(org.sandev.basics.structs.SandMessage sandMessage)
          Serialize a SandMessage as a text string for inclusion in a JMS TextMessage.
 void setCharSerializer(org.sandev.basics.nodecommon.CharSerializer val)
          mutator for charSerializer
 void shutdown()
           
 
Methods inherited from class org.sandev.tools.util.DirectCallMessager
addSubscriber, doSend, error, getMessageable, getOurInstance, getOurNode, getSubTable, getWireSecurity, info, query, queryReceiver, removeSubscriber, send, setOurInstance, setOurNode, subscribe, unsubscribe
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected javax.naming.Context context

connection

protected javax.jms.TopicConnection connection
Single connection to the provider is all we need


session

protected javax.jms.TopicSession session
Use a single TopicSession since we're not using transactions


subscriberMap

protected java.util.Hashtable subscriberMap
Map of topic name to TopicSubscribers


charSerializer

protected org.sandev.basics.nodecommon.CharSerializer charSerializer
The CharSerializer we use for converting objects to text and back.

Constructor Detail

JMSMessager

public JMSMessager()
Default ctor does nothing.

Method Detail

getCharSerializer

public org.sandev.basics.nodecommon.CharSerializer getCharSerializer()
accessor for charSerializer


setCharSerializer

public void setCharSerializer(org.sandev.basics.nodecommon.CharSerializer val)
mutator for charSerializer


init

public void init(org.sandev.basics.nodecommon.Messageable node,
                 org.sandev.basics.structs.NodeInstance instance)
          throws org.sandev.basics.nodecommon.MessagerException
Specified by:
init in interface org.sandev.basics.nodecommon.Messager
Overrides:
init in class DirectCallMessager
Throws:
org.sandev.basics.nodecommon.MessagerException

shutdown

public void shutdown()
Specified by:
shutdown in interface org.sandev.basics.nodecommon.Messager
Overrides:
shutdown in class DirectCallMessager

onMessage

public void onMessage(javax.jms.Message message)
Receive a JMS Message, unpack it, and pass it to the node. If the JMS message has a replyTo destination set, treats the message as a query and passes it to the node's query method. Otherwise passes the message to the node's deliver method.

Specified by:
onMessage in interface javax.jms.MessageListener

getImplementationWireSecurity

protected int getImplementationWireSecurity()
JMS doesn't specify wire-level security (although some providers support secure transports like SSL).

Overrides:
getImplementationWireSecurity in class DirectCallMessager

doSubscribe

protected void doSubscribe(org.sandev.basics.nodecommon.Messageable node,
                           java.lang.String msgClass,
                           java.lang.String sender,
                           int optimize)
                    throws org.sandev.basics.nodecommon.MessagerException
Make the subscription happen.

Overrides:
doSubscribe in class DirectCallMessager
Throws:
org.sandev.basics.nodecommon.MessagerException

doUnsubscribe

protected void doUnsubscribe(org.sandev.basics.nodecommon.Messageable node,
                             java.lang.String msgClass,
                             java.lang.String sender,
                             int optimize)
                      throws org.sandev.basics.nodecommon.MessagerException
Make the subscription go away.

Overrides:
doUnsubscribe in class DirectCallMessager
Throws:
org.sandev.basics.nodecommon.MessagerException

doSend

protected void doSend(org.sandev.basics.structs.SandMessage msg,
                      int optimize)
               throws org.sandev.basics.nodecommon.MessagerException
Send the message.

Overrides:
doSend in class DirectCallMessager
Throws:
org.sandev.basics.nodecommon.MessagerException

doSend

protected void doSend(org.sandev.basics.structs.SandMessage msg,
                      java.lang.String topicName)
               throws org.sandev.basics.nodecommon.MessagerException
Send a SandMessage to the specified JMS topic.

Throws:
org.sandev.basics.nodecommon.MessagerException

doQuery

protected org.sandev.basics.structs.SandMessage doQuery(org.sandev.basics.structs.SandMessage msg,
                                                        java.lang.String recipient,
                                                        int optimize)
                                                 throws org.sandev.basics.nodecommon.MessagerException
Query the specified node.

Overrides:
doQuery in class DirectCallMessager
Throws:
org.sandev.basics.nodecommon.MessagerException

doQuery

protected org.sandev.basics.structs.SandMessage doQuery(org.sandev.basics.structs.SandMessage msg,
                                                        java.lang.String topicName)
                                                 throws org.sandev.basics.nodecommon.MessagerException
Perform a synchronous query on the given Topic. Creates a temporary topic and waits for the response on that topic.

Throws:
org.sandev.basics.nodecommon.MessagerException

doReceive

protected void doReceive(javax.jms.Message message)
                  throws javax.jms.JMSException,
                         java.io.IOException,
                         org.sandev.basics.structs.SandException
Deserialize a JMS message, pass it to the node's receive method, and return the response to the Topic specified in the JMS message's replyTo field.

Throws:
javax.jms.JMSException
java.io.IOException
org.sandev.basics.structs.SandException

doDeliver

protected void doDeliver(javax.jms.Message message)
                  throws javax.jms.JMSException,
                         java.io.IOException,
                         org.sandev.basics.structs.SandException
Deserialize a JMS message and pass it to the node's deliver method.

Throws:
javax.jms.JMSException
java.io.IOException
org.sandev.basics.structs.SandException

getContext

protected javax.naming.Context getContext()
                                   throws org.sandev.basics.nodecommon.MessagerException
Create a JNDI Context for JMS Topic lookups. With OpenJMS, this uses OpenJMS JNDI provider because the OpenJMS server needs to be restarted independently of the container JNDI server.

Throws:
org.sandev.basics.nodecommon.MessagerException

createTopicConnection

protected javax.jms.TopicConnection createTopicConnection()
                                                   throws org.sandev.basics.nodecommon.MessagerException
Create and start a JMS TopicConnection.

Throws:
org.sandev.basics.nodecommon.MessagerException

createSession

protected javax.jms.TopicSession createSession()
                                        throws org.sandev.basics.nodecommon.MessagerException
Create a JMS TopicSession.

Throws:
org.sandev.basics.nodecommon.MessagerException

getTopic

protected javax.jms.Topic getTopic(java.lang.String topic)
                            throws javax.naming.NamingException
Lookup the JMS Topic with the given topic name.

Throws:
javax.naming.NamingException

jmsSubscribe

protected void jmsSubscribe(java.lang.String topicName)
                     throws org.sandev.basics.nodecommon.MessagerException
Subscribe this messager to a JMS topic.

Throws:
org.sandev.basics.nodecommon.MessagerException

jmsUnsubscribe

protected void jmsUnsubscribe(java.lang.String topicName)
                       throws org.sandev.basics.nodecommon.MessagerException
Unsubscribe this messager from a JMS topic. The messager must already be a subscriber to the topic.

Throws:
org.sandev.basics.nodecommon.MessagerException

serializeSandMessage

protected java.lang.String serializeSandMessage(org.sandev.basics.structs.SandMessage sandMessage)
                                         throws java.io.IOException
Serialize a SandMessage as a text string for inclusion in a JMS TextMessage.

Throws:
java.io.IOException

deserializeJMSMessage

protected org.sandev.basics.structs.SandMessage deserializeJMSMessage(javax.jms.TextMessage message)
                                                               throws javax.jms.JMSException,
                                                                      java.io.IOException
Deserialize a SandMessage serialized as a JMS TextMessage.

Throws:
javax.jms.JMSException
java.io.IOException