How To Add A Robot

I find it extremely convenient to get the functionality working with a stand-alone main program before trying to integrate it. I also find it easiest to start from a working example and modify what's required. So...

Copy one of the other robots to a new directory. Start with the weather example if the robot interacts with the web, or use the word example if it accesses a database.

It seems like a good idea to isolate the email/jms aspect of the robot from the worker bee part of it so I recommend two or more classes:

  • 1) The message bean itself that derives from the Robot class.
  • 2) The task oriented bean that gets used by the above.
  • Start with the worker bean (WeatherBean or DictionaryBean) and chop out everything but the methods setText(), getText(), help(), toString() and main(). Adjust this class until:

  • setText() accepts the free form input expected in the body of the message or the one word "help"
  • getText() returns the desired contents of the email response message or the help() text if setText() was called with "help"
  • help() returns the help text
  • toString() returns the response email subject line
  • main() excercises this functionality
  • OK, so now work on the robot class (copied from either WeatherMan or Dictionary, they are pretty much the same). They are fairly simple so not much can go wrong. In the onMessage() method, invoke the worker bean and call replyMessage () with the subject and response text. Override the ejbCreate() and ejbRemove() methods to set up and tear down anything necessary.

    You can't really test this without deploying it so that's the next step. Call up DeployTool and create a topic. This is used below. The naming scheme I used was jms/xxxx where xxxx is usually the same as the bean. Open the Email Answering Service .ear file and say you want a new Enterprise Bean. Choose 'create a new jar' and give it a display name (unfortunately there's no way to name the jar when you do this). In the contents section add your classes plus supporting ones like WebBean and Robot. You can give a description too if you want.

    Next choose Message-Driven bean type and for the 'Enterprise Bean class' choose the class that extends Robot. You can give a description too if you want.

    Next choose 'Bean Managed' transactions, (that means none since we ignore them).

    Next choose a 'Topic' destination type and choose the topic created above as the 'destination'.

    Skip to the 'Resource Factories' page and create an entry here for the jms/QueueConnectionFactory with that as the jndi name, type javax.jms.QueueConnectionFactory, container authentication and shareable. I don't think it uses the user name and password.

    Next add jmsOutbox as a resource environment reference with a coded and jndi name of jms/Outbox and type javax.jms.Queue.

    Next you need to create a role to run as, and select it.

    Adjust the EmailDispatcherBean Environment entry for "Subject Topic Map" to add the mapping between the email subject and the topic created above. Also add the topic as a Resource Environment Reference with the type Topic.

    Redeploy the Email Answering Service and test it.