I always prefer to use maven plug-ins any where in my development cycle, recently I studied about JMS programming with ActiveMQ. Once I familiar with basic of ActiveMq, as usually I want to run ActiveMQ as a maven plug-in as same way as Jetty plug-in, fortunately there is very cool maven plug in shiped with ActiveMQ. Here I describe how to create very simple JMS application and test it using this plug-in.
Step-1
Create a Maven project using your Java IDE, in my case it is Eclipse and modify POM file as given below.
<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>sample</groupId>
<artifactId>jms-maven-test</artifactId>
<version>1.0.0</version>
<description></description>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>maven-activemq-plugin</artifactId>
<version>5.2.0</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
</project>
Maven-activemq-plugin doesn’t not require any dependency, Here, activemq-core and JUnit are used for our test client program.
Step -2
Create a simple test program to send simple massage to a message queue. I have created my sample application as JUnit test case .
public class SimpleTest extends TestCase {
private static String user = ActiveMQConnection.DEFAULT_USER;
private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "TOOL.SAGARA.eg1";
public void testMessageDelivery() throws Exception {
Connection connection = null;
Destination destination = null;
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
connection.start();
// Create the session
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
//
destination = session.createQueue(subject);
// Create the producer.
MessageConsumer consumer = session.createConsumer(destination);
Message msg = consumer.receive();
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
System.out.println("recived message: " + txtMsg.getText());
}
System.out.println("Done.");
}
protected void setUp() throws Exception {
super.setUp();
Connection connection = null;
Destination destination = null;
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
connection.start();
// Create the session
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
//
destination = session.createQueue(subject);
// Create the producer.
MessageProducer producer = session.createProducer(destination);
// Start sending messages
String msgTo = " Hello world ActiveMQ from Sagara :: Maven";
TextMessage message = session.createTextMessage(msgTo);
System.out.println("Sending message: " + msgTo);
producer.send(message);
System.out.println("Done.");
connection.close();
}
}
Step -3
Open a console and run maven-activemq-plugin as follows or you can use you IDE features to run this Maven goal , also make sure to use test skip option otherwise our test case try to connect to a message broker during test run phase.
mvn install activemq:run -Dmaven.test.skip
Step- 4
In your IDE run the test case as Junit test case……………….. you can see the results!
This is a very basic example but you can configure maven-activemq-plugin as give below for advanced tasks.
<configuration>
<configUri>
xbean:file:src/main/resources/activemq.xml
</configUri>
<fork>false</fork>
<systemProperties>
<property>
<name>javax.net.ssl.keyStorePassword</name>
<value>password</value>
</property>
<property>
<name>org.apache.activemq.default.directory.prefix</name>
<value>./target/</value>
</property>
</systemProperties>
</configuration>
Thursday, February 26, 2009
Thursday, February 19, 2009
Axis2 Module and Spring
It is possible to access Spring beans within Axis2 services using “ServiceObjectSupplier” parameter very easily, but it is not possible use this approach to access Spring context within a Axis2 Handler. Here I have listed some of the alternative ways to access Spring context from Axis2 Handler invoke method.
- When the time of handler invoke method calls, if Spring context is not available, you can use flowing approach to instantiate Spring context for the first time with handler's init method. This is very familiar to spring users.
public void init(ConfigurationContext configContext, AxisModule module)throws AxisFault {
ClassLoader classLoader = getClass().getClassLoader();
ClassPathXmlApplicationContext appCtx = new
ClassPathXmlApplicationContext(new String[]
{"applicationContext.xml"}, false);
appCtx.setClassLoader(classLoader);
appCtx.refresh();
} - If Spring context is already available and you just want to access those spring beans within your Handler invoke method you can use following approach, this will not re-instantiate Spring context, instead this facilitate to access available Spring context through ServletContext and MessageContext. If you plan to use axis2 as part of an enterprise application and deploy services also as a WAR file this approach is much more appropriate for you.
public InvocationResponse invoke(MessageContext messageContext)throws AxisFault {
ServletContext servletContext = (ServletContext) messageContext.
getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);
WebApplicationContext wctx =
WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
SpringService service=(SpringService)wctx.getBean("springService");
- - - -
- - -
}
Subscribe to:
Posts (Atom)