Sunday, March 8, 2009

Setting up SVN for Netbeans 6.1

Easy steps to setup SVN for Netbeans IDE 6.1:

Download
1- svn-win32-1.4.6
2- TortoiseSVN-1.4.5.10425-win32-svn-1.4.5

--:Setup steps:--

  • Unzip it there
  • Setting up the CVS
Add c:\svn-win32-1.4.6\bin to Path (Right click MyComputer >> Properties >> System >> Advanced System Settings >> Environment Variables >> Path variable >> append (c:\svn-win32-1.4.6\bin) to it.

  • Now creating repository.
  1. Create a folder named My-Repository for example
  2. Right click anywhere over the folder window
  3. TortoiseSVN >> Create Repository Here >> Select FSFS Native File System The Repository was successfully created.
  • Applying versioning to your project
  1. Open Netbeans 6.1 IDE, select your project
  2. Versioning >> Import into Subversion repository
  3. Enter following into text box:
  4. file:///c:/My-Repository
  5. (My-Repository is the repository name you just created in step 5.)
  6. Your repository folder name is already entered there.
  7. You need to put some comment message over the text box. Click Finish
  8. All the files in your project is not appended to the repository, now you need to commit it.
  • Right click on your Project >> Versioning >> Commit
You are done!!!
Note: Now if any change comes on any of your files of your project, it will be indicated showing the version difference with the repository.

Enjoy!!!

Saturday, March 7, 2009

Message Driven Bean With GlassFish-V2 & Netbeans 6.1

Required JAR files to be included:
• GLASSFISHV2_HOME/lib/appserv-admin.jar
• GLASSFISHV2_HOME/lib/appserv-rt.jar
• GLASSFISHV2_HOME/lib/javaee.jar
• GLASSFISHV2_HOME/install/applications/jmsra/imqjmsra.jar

New Enterprise Project Creation:
File >> New Project >> Enterprise >> Enterprise Application >> Enter the name of the application. (Ventura is my Project Name) >> Click on Finish
It creates three modules named:
1- Ventura
2- Ventura-ear
3- Ventura-war

Starting the GlasshFish Application Server:
1- Go the Services Tab
2- Navigate to Servers > GlassFish V2 > Right Click > Start

When the server gets started, right click on the GlassFishv2 >> View Admin Console
Browser is launched>> Enter the user name [admin], and password [adminadmin] default
setting.

Creating connection factory & Message Destination
1- Navigate to Resources >> JMS Resources >> Connection Factories >> New >>
Enter the JNDI Name (jms/NewConnectionFactory)
Resource Type : Select javax.jms.ConnectionFactory
Leave all else as default setting. Click on OK (ConnectionFactory is created.)

2- Navigate to the Resources >> Destination Resources >> New >>
Enter the JNDI name (jms/topic)
Resource Type : Select javax.jms.Topic

1- New Destination
2- Destination Name = jms/topic
3- Physical Destination name (newtopic)
4- Source Type = javax.jms.Topic
Leave all else as default setting. Click on OK (New Destination Resource is created)

Creating Message Driven Bean
1- Create package eg. mdb
2- Create a Message Driven Bean Enter the MDB name (mine is LogBean.java)
3- Set Project Destination javax.jms.Topic
4- Click on OK

You Get the following automatically generated MDB code (definition to onMessage(Message message) method is given by me.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mdb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
*
* @author Chandra
*/
@MessageDriven(mappedName = "jms/topic", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "clientId", propertyValue = "TopicBeanBean"),
@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "TopicBeanBean")
})
public class TopicBeanBean implements MessageListener {

public TopicBeanBean() {
}

public void onMessage(Message message) {
if(message instanceof MapMessage){
MapMessage mapMessage = (MapMessage)message;
try {
String name = mapMessage.getString("name");
String address = mapMessage.getString("address");
String contact_no = mapMessage.getString("contact_no");
System.out.println("Message Received : Name= " + name + " Address= " + address + " Contact No= " + contact_no);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
}
}

Creating the Client for Topic generator
1- Create a new class named LogClient.java for example
2- Inside the main method put the following code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mdb;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
*
* @author Chandra
*/
public class TopicClient {

public static void main(String[] args) {
try {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)ctx.lookup("jms/NewMessageFactory");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) ctx.lookup("jms/topic");
MessageProducer producer = session.createProducer(topic);
MapMessage mapMessage = session.createMapMessage();

mapMessage.setString("name", "Chandra");
mapMessage.setString("address", "Bangalore");
mapMessage.setString("contact_no", "123456789");
producer.send(mapMessage);
producer.close();
connection.close();

} catch (NamingException ne) {
ne.printStackTrace();
} catch(JMSException jmse){
jmse.printStackTrace();
}


}
}


Building & Running:
Now you are ready to deploy and test your Message Driven Bean:
Right Click on Ventura >> Clean & Build >> UnDeploy & Deploy >> Run

When the application is Up & Running you can run the TopicClient.
See the output in the Server's log file.
Message Received : Name= Chandra Address= Bangalore Contact No= 12345678


Enjoy!!!

Saturday, December 20, 2008

EJB 3.0 Power of Interceptor

Requirements:
  1. Installled JDK 1.5 (better 1.6)
  2. An IDE of your choice e.g. vi, emacs, netbeans 6.1/6.5 (SE or EE), Eclipse Ganymede (SE or EE)
  3. @Stateless, @Local Annotations in classpath
  4. An Java EE 5 capable application server of your choice. It will work with Glassfish v1+ (better v2)

What is to do:

  1. Create and Deploy a simple Stateless or Stateful Session Bean.
  2. Create a simple Java class with one method with the following signature: public Object (InvocationContext context) throws Exception:

    import javax.interceptor.AroundInvoke;
    import javax.interceptor.InvocationContext;
    public class TracingInterceptor {

    @AroundInvoke
    public Object logCall(InvocationContext context) throws Exception{
    System.out.println("Invoking method: " + context.getMethod());
    return context.proceed();
    }
    }
  3. The method has to be annotated with @AroundInvoke. It is the one and only available annotation.
  4. Inside the method you can "decorate" existing functionality. The invocation context.proceed() invokes the actual method and returns the value. An interceptor wraps the method completely.
  5. Apply the interceptor on any Session Bean you like e.g.:

    @Interceptors(TracingInterceptor.class)
    @Stateless
    public class HelloWorldBean implements HelloWorld {

    public void sayHello() {
    System.out.println("Hello!");
    }
    }
  6. The annotation @Interceptors can be applied for the whole class, or chosen methods. You can even exclude interceptors with @ExcludeClassInterceptors - but it is rarely needed.
  7. Compile everything and JAR the output (in Netbeans just "build", in Eclipse "Export -> JAR")
  8. Copy the JAR into the autodeploy folder of WLS 10 (bea10\user_projects\domains\YOUR_DOMAIN\autodeploy), or glassfish\domains\domain1\autodeploy in the case of Glassfish v2, or jboss-4.2.2.GA\server\default\deploy in case of JBoss
  9. Inspect the log files, you are done :-)

What you have gained:

  1. There is no XML needed - its DRY.
  2. Its robust - compiler checks for the existence of the Interceptor and checks the right spelling of the annotation etc.
  3. Cross cutting functionality can be easily factored out into reusable interceptors.
  4. DI works in interceptors. You can easily inject resources or other beans into an interceptor.
  5. The whole method is wrapped - you have full access to the parameters and return values. You can even reexcute the method or not do it at all (for caching purposes).
  6. It's self documented: there is no surprise - the annotation is visible in code.
  7. They are portable and run on every Java EE 5 compliant application server.
  8. No additional frameworks, libraries etc. are need. This is good for maintenance.
  9. Its flexible - if you prefer XML - no problem just configure the decoration in a XML-descriptor: