DropboxMQ 是一个使用文件系统来存储和发布消息的 JMS 服务器。
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 Dropbox
Creating 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
create-dropbox script as described in the Administrative Guide.
2. Gathering the Jars
DropboxMQ only requires three jars.
commons-logging-1.1.jar are all included in the DropboxMQ download.
3. JNDI Lookups
A JNDI lookup is the preferred method for retrieving
Destination objects. The first step of a JNDI lookup is creating a
InitialContext and the environment properties provided to the
InitialContext constructor are the primary mechanism for configuring DropboxMQ. See the complete list of JNDI properties on the Configuration Guide.
Properties properties = new Properties();// properties.load() would probably be more suitable properties.setProperty( Context.INITIAL_CONTEXT_FACTORY, "net.sf.dropboxmq.jndi.InitialContextFactoryImpl" ); properties.setProperty( "net.sf.dropboxmq.root", "/dropboxmq" ); InitialContext initialContext = new InitialContext( properties ); ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup( "ConnectionFactory" ); Destination destination = (Destination)initialContext.lookup( "TestQueue1" );
4. Creating a
Connection and a
Connection connection = connectionFactory.createConnection(); Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE );
Producing a Message
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
Consuming a Message
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.