Developing JMS application using Spring 2.5.3 and Activemq 4.0 in Netbeans 6.1
1.Download Spring 2.5.3, and Activemq 4.0 and Netbeans.
2.Unpack Spring, Activemq and install Netbeans 6.1
3.Creating a project in netbeans 6.1
-Go to File->New project(Ctrl+Shift+N), Under categories select Java->Java Application
Project Name “ExampleJMS”
-Uncheck “Create Main Class”
-Then click “Finish”
4.Navigate to these files and add to the libraries (Righ click “Libraries” and select “Add JAR/folder…..”
*commons-lang.jar, common-beanutils.jar, commons-digester.jar, commons-logging.jar: \spring-framework-2.5.3\lib\jakarta-commons
spring.jar :\spring-framework-2.5.3\dist
jms.jar :\spring-framework-2.5.3\libj2ee
incubator-activemq-4.0.jar :\incubator-activemq-4.0
Ready to code.
5.Right click “ExampleJMS” and select New->Java Interface. Type IExampleSender for class name and any for package name(eg. org.fanosft).Edit the file to be as shown below
6.Right click “ExampleJMS” and select New->Java Class. Type ExampleSender for class name and choose your package name(eg. org.fanosft)
Edit the file to be as shown below:
7.We are done with the functional part, now how does Spring knows about it.
Right click the project , select New->Other->Other->Spring XML Configuration File
Click Next and Type jms-application for “File Name”
Click Next and check “p”.Click “Finish”
8.Look in the ExampleSender.java file the property jmsTemplate needs a connection to the Broker(ie Activemq).
Lets decare a bean id= “connectionFactory” and class=”org.apache.activemq.ActiveMQConnectionFactory” in xml file.
URL to the broker “tcp://localhost:61616″ that is the default.
Change the url if yours is different.
9.In real life every mail needs an address,so configure an address for the jmsTemplate
Just as in 6 but this time id=”jmsdest” class=”org.apache.activemq.command.ActiveMQQueue”.
Set the physicalName=”example.queue”
10.Now we have our address and the Broker(for delivery)
Create bean id=”jmsTemplate” class=”org.springframework.jms.core.JmsTemplate”
Create references to “jmsdest” and “connectionFactory”
11.Create a bean for ExampleSender with id=”sender” class=”org.fansoft.ExampleSender”.
Since ExampleSender makes use “jmsTemplate”, we have to wire it to the jmsTemplate and through this the sender will know how to send the message.
12.How the message will be sent is now configure but how does the message get read.
Since we don’t want to wait for the message to arrive.Let somebody do the work.
Create a class class “ClientMain” that implements MessageListener as shown below

Then create a bean with id=”client” and class=”org.fansoft.ClientMain”
13.The ClientMain cannot work without a message listener container.It does a lot of work:-
watches the destination(address) ,waiting for message to arrive.
14.Create the final class ServerMain as shown below
15.Go to incubator-activemq-4.0\bin and run activemq.bat(Windows) or likely activemq.sh(Linux)
If you did get some errors ,this is my configuration file from incubator-activemq-4.0/conf
<!– START SNIPPET: xbean –>
<beans xmlns=”http://activemq.org/config/1.0″>
<broker useJmx=”true”>
<!– Use the following to set the broker memory limit (in bytes)–>
<memoryManager>
<usageManager id=”memory-manager” limit=”1048576″/>
</memoryManager>
<!– Use the following to configure how ActiveMQ is exposed in JMX–>
<managementContext>
<managementContext connectorPort=”1099″ jmxDomainName=”org.apache.activemq”/>
</managementContext>
<!– In ActiveMQ 4, you can setup destination policies –>
<destinationPolicy>
<policyMap><policyEntries>
<policyEntry topic=”FOO.>”>
<dispatchPolicy>
<strictOrderDispatchPolicy />
</dispatchPolicy>
<subscriptionRecoveryPolicy>
<lastImageSubscriptionRecoveryPolicy />
</subscriptionRecoveryPolicy>
</policyEntry>
</policyEntries></policyMap>
</destinationPolicy>
<persistenceAdapter>
<journaledJDBC journalLogFiles=”5″ dataDirectory=”../activemq-data”/>
<!– To use a different datasource, use th following syntax : –>
<!– <journaledJDBC journalLogFiles=”5″ dataDirectory=”../activemq-data” dataSource=”#mysql-ds”/> –>
</persistenceAdapter>
<transportConnectors>
<transportConnector name=”default” uri=”tcp://localhost:61616″ />
<transportConnector name=”stomp” uri=”stomp://localhost:61613″/>
</transportConnectors>
<networkConnectors>
<!– by default just auto discover the other brokers –>
<!– <networkConnector name=”default” uri=”multicast://default”/> –>
<networkConnector name=”localhost” uri=”static://(tcp://localhost:61616)” failover=”true”/>
</networkConnectors>
</broker>
<!– This xbean configuration file supports all the standard spring xml configuration options –>
<!– Postgres DataSource Sample Setup –>
<!–
<bean id=”postgres-ds” class=”org.postgresql.ds.PGPoolingDataSource”>
<property name=”serverName” value=”localhost”/>
<property name=”databaseName” value=”activemq”/>
<property name=”portNumber” value=”0″/>
<property name=”user” value=”activemq”/>
<property name=”password” value=”activemq”/>
<property name=”dataSourceName” value=”postgres”/>
<property name=”initialConnections” value=”1″/>
<property name=”maxConnections” value=”10″/>
</bean>
–>
<!– MySql DataSource Sample Setup –>
<!–
<bean id=”mysql-ds” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
<property name=”driverClassName” value=”com.mysql.jdbc.Driver”/>
<property name=”url” value=”jdbc:mysql://localhost/activemq?relaxAutoCommit=true”/>
<property name=”username” value=”root”/>
<property name=”password” value=”frankappiah”/>
<property name=”poolPreparedStatements” value=”true”/>
</bean>
–>
<!– Embedded Derby DataSource Sample Setup –>
<!–
<bean id=”derby-ds” class=”org.apache.derby.jdbc.EmbeddedDataSource”>
<property name=”databaseName” value=”derbydb”/>
<property name=”createDatabase” value=”create”/>
</bean>
–>
</beans>
<!– END SNIPPET: xbean –>
16.Run the application.
Output:
Sending message
Receiving message
Hello Welcome
17.It is not desirable to start Activemq manually so go to ServerMain and uncomment the commented code.
This will start Activemq from code.Please close the running Broker service in 13.





hello all
i want to create a jms application but i m unable to create the environment..
any one can help me how to create the jms application using netbeans and how to setup the message server.
kindly help me.
i want to create the jms application without spring..
becoz i do’t know spring……
you don’t have to know spring to create a jms application.what i created on the blog is some sort
of standalone with spring but you actually create one with activemq itself.That means getting some resource at Activemq
web site.
I will post one tutorial on Saturday so watch out.