JUnit test classes can be generated automatically using the Eclipse plugin attached.
Get Fast code plugin from http://fast-code.sourceforge.net/
Drop the plugin in to the plugins folder of your eclipse. Restart the Eclipse.
A new option called fast code should be added in the tool bar.
In Eclipse go to Windows -> preferences -> fast code preferences.
Select the Junit preferences page option. Retain the default values and select the "Test Location" from the drop down.
The drop down gets populated from the "sources" tab of the “Java build path” of Eclipse.
If you are working on creating the GUI test classes the drop down should have "${project}/GUI/Test".
Similarly when you are creating test classes for service the drop down values has to change.
The same package structure of the source file is retained in the test class.
The Fast code creates a skeleton Junit test class for the source file.
For more information on Fast code : http://fast-code.sourceforge.net/documentation.htm
Thursday, August 12, 2010
Tuesday, May 18, 2010
Message Driven POJO
MDP stands for Message Driven Pojo. MDP is Spring implementation for asynchronous
sending and recieving of jms messages. It sounds very similar to Message Driven
Beans of EJB specification, but there are some differences with both the approaches,
1. In case of MDB only specialized beans can listen to the JMS messages whereas in MDP, even a normal POJO can be configured to listen to JMS messages asynchronously
2. MDP requres an EJB runtime. MDP can even run as a standalone program.
We will discuss a brief example on how to setup an MDP for asynchronous message reception.
We need following things setup before implementing mdp,
1. A JMS Queue - Queue holds the messages sent by producer. We will use Apache ActiveMQ for Queue implementation
2. A producer - a message producer which sends messages to the queue
3. A consumer - program that listens to the Queue
Setting up Queue:-
Download the latest Apache ActiveMQ distribution from site.
Unzip the zip and configure required environment parameters
Go to ActiveMQ admin console, http://localhost:8161/admin/index.jsp
Go to queues and create a test queue, test.q
The producer:-
The class org.springframework.jms.core.JmsTemplate is used to send and recieve jms messages.
Create a class JmsSender
package com.tlr.da.mdp;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class JMSSender {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public Destination getDestination() {
return destination;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
private Destination destination;
public void send(final String message){
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText(message);
return textMessage;
}
});
}
}
Now we will create a receiver to receive the messages sent by sender,
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
public class JMSReceiver{
public JMSReceiver(){
}
public void receive(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.peintln("message received");
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Now we will see the Spring configurations needed for this MDP example.
tcp://localhost:61616
Configure the Connection Factory for JMS. The broker URL is where the JMS Queues are accessible.
Configure the Queue destination.
JMSTemplate configuration.
Configuration for the JMSSender. The destination is testq.
The DefaultMessageListenerContainer is the JMS Container that manages the MessageListener. In our example pupePojo is the messageListener that reads the JMS messages.
We can specify the number of concurrent listeners for a single Queue. This is done by specifying the "maxConcurrentConsumers" property.
Our JMSReceiver bean.
This is actual configuration of our MessageListener. Note that JMSReceiver does not implement the JMS MessageListener interface. In case of EJB you have to implement the MEssageListener and override onMessage() method.
This is achieved by a special class provided by Spring, MessageListenerAdapter. MessageListenerAdapter allows to declare any class a message listener.
The POJO that where you want to handle the message, is passed as a delegate. The method that needs to be called upon arrival of message is configured as defaultListenerMethod.
In this case when JMS message comes to test.q, receive method of JMSReceiver is called.
To run above example, in a main method, get context for spring.xml, get JMSSender instance and send a message. You can print the message in JMSReciever or monitor the ActiveMQ console to check if the message is consumed.
sending and recieving of jms messages. It sounds very similar to Message Driven
Beans of EJB specification, but there are some differences with both the approaches,
1. In case of MDB only specialized beans can listen to the JMS messages whereas in MDP, even a normal POJO can be configured to listen to JMS messages asynchronously
2. MDP requres an EJB runtime. MDP can even run as a standalone program.
We will discuss a brief example on how to setup an MDP for asynchronous message reception.
We need following things setup before implementing mdp,
1. A JMS Queue - Queue holds the messages sent by producer. We will use Apache ActiveMQ for Queue implementation
2. A producer - a message producer which sends messages to the queue
3. A consumer - program that listens to the Queue
Setting up Queue:-
Download the latest Apache ActiveMQ distribution from site.
Unzip the zip and configure required environment parameters
Go to ActiveMQ admin console, http://localhost:8161/admin/index.jsp
Go to queues and create a test queue, test.q
The producer:-
The class org.springframework.jms.core.JmsTemplate is used to send and recieve jms messages.
Create a class JmsSender
package com.tlr.da.mdp;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class JMSSender {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public Destination getDestination() {
return destination;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
private Destination destination;
public void send(final String message){
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText(message);
return textMessage;
}
});
}
}
Now we will create a receiver to receive the messages sent by sender,
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
public class JMSReceiver{
public JMSReceiver(){
}
public void receive(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.peintln("message received");
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Now we will see the Spring configurations needed for this MDP example.
Configure the Connection Factory for JMS. The broker URL is where the JMS Queues are accessible.
This is actual configuration of our MessageListener. Note that JMSReceiver does not implement the JMS MessageListener interface. In case of EJB you have to implement the MEssageListener and override onMessage() method.
This is achieved by a special class provided by Spring, MessageListenerAdapter. MessageListenerAdapter allows to declare any class a message listener.
The POJO that where you want to handle the message, is passed as a delegate. The method that needs to be called upon arrival of message is configured as defaultListenerMethod.
In this case when JMS message comes to test.q, receive method of JMSReceiver is called.
To run above example, in a main method, get context for spring.xml, get JMSSender instance and send a message. You can print the message in JMSReciever or monitor the ActiveMQ console to check if the message is consumed.
Wednesday, April 14, 2010
Checking Oracle version
Often it is necessary to checking oracle database version you are working on. There are several ways of checking it,
1. If you just want to check the version information of the Oracle database, simply connect and login to the Oracle database with SQL *Plus. Upon login, you will see:
SQL*Plus: Release 9.2.0.6.0 – Production on Tue Oct 18 17:58:57 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6.0 – 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.6.0 – Production
The first italic number is the version of the SQL*Plus client and the second italic number is the version of Oracle database which you are connected to.
2. You can use following command to retrieve the version information,
select * from sys.v_$version
This will work with oracle 9 and above. The query will return,
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
CORE11.1.0.7.0 Production"
TNS for Linux: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
1. If you just want to check the version information of the Oracle database, simply connect and login to the Oracle database with SQL *Plus. Upon login, you will see:
SQL*Plus: Release 9.2.0.6.0 – Production on Tue Oct 18 17:58:57 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6.0 – 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.6.0 – Production
The first italic number is the version of the SQL*Plus client and the second italic number is the version of Oracle database which you are connected to.
2. You can use following command to retrieve the version information,
select * from sys.v_$version
This will work with oracle 9 and above. The query will return,
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
CORE11.1.0.7.0 Production"
TNS for Linux: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
Subscribe to:
Posts (Atom)