Producing or Consuming Messages (Common Tasks)Whether producing or consuming messages, DropboxMQ applications need to do some common tasks before getting to the nuts and bolts of producing or consuming. 1. Creating a DropboxCreating a dropbox is as easy as creating a handful of directories in a shared directory known as
the root directory. Still the preferred method for creating both queue and topic dropboxes is to
use the 2. Gathering the JarsDropboxMQ only requires three jars. 3. JNDI LookupsA JNDI lookup is the preferred method for retrieving
4. Creating a
|
Connection connection = connectionFactory.createConnection(); Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE ); |
MessageProducer producer = session.createProducer( destination ); TextMessage message = session.createTextMessage( "Hello World!" ); producer.send( message ); |
After executing the above code, you will notice a new file in
/dropboxmq/TestQueue1/incoming/target. The name of file will be something similar to
4.1140911283485000-1299715532.T. For an explaination of each of the fields contained
in the filename look here. Also note that the contents
of the file are exactly the text of the TextMessage
("Hello World!").
connection.start(); MessageConsumer consumer = session.createConsumer( destination ); TextMessage message = (TextMessage)consumer.receiveNoWait(); System.out.println( message == null ? "No message found" : message.getText() ); |
That's it! You will notice that after executing this code the message file that was in the
target directory is now in the processed directory. For a complete and
compile-able listing of the code above look
here.