Tuesday, April 12, 2011

Spring Integration file Inbound/Outbound adapter and Filename generator

This example explains how to modify, rename and move the file to destination folder using Sprig Integration(SI) file:inbound adapter and file:outbound adapter.

SI provides Inbound and outbound adapter for many systems like File system, JMS, UDP , TCP, Mail Web Services, JDBC and RMI.
Here we are going to see the example of File System.

Step 1. create a spring context file. spring-context.xml
 <?xml version ="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:file="http://www.springframework.org/schema/integration/file"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/file
            http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">


<file:inbound-channel-adapter id="incomingFiles"
                                  directory="file:C:\Temp\InboundLocation>
        <int:poller id="poller" fixed-delay="5000"/>
    </file:inbound-channel-adapter>

    <file:outbound-channel-adapter id="outgoingFiles"
                                   directory="file:C:\Temp\OutboundLocation"   delete-source-files="true" filename-generator ="filenameGenerator"/>

    <bean id="filenameGenerator" class="com.test.si.FilenameGenerator"/>
</beans>
The poller look into in InboundLocation for each 5 seconds, if any new files found then modify the file content if required and rename the file to ' NewFile' moves to  OutboundLocation then deletes the original file.
prevent-duplicates=true by default this property is false, enabling this property it prevents moving duplicates.
filename-pattern="*.TXT" filters to only specific extension files.

Add these properties to In-bound adapter.
Step 2. Create file name generator class

package com.test.si;
import java.io.File;

import org.springframework.integration.Message;
import org.springframework.integration.file.DefaultFileNameGenerator;


public class FilenameGenerator extends  DefaultFileNameGenerator
{
    public FilenameGenerator() {
        super();
    }
    @Override
    public String generateFileName(Message<?> message) {
        File inputFile = (File)message.getPayload();
        String originalFileName = super.generateFileName(message);
        System.out.println("File Name: " + originalFileName);
        //TODO: Read the file
        //TODO: Write the file
        String newFileName = "NewFile";
        return newFileName;
    }
}


Step 3. run the example.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class testSpringIntegration {
    public testSpringIntegration() {
        super();
    }

    public static void main(String[] args) {
        testSpringIntegration testSpringIntegration = new testSpringIntegration();
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
              
    }
}





Saturday, March 26, 2011

Step by Step EJB 3 State less session bean


Required java version JDK 1.5
javax.ejb_3.0.1.jar

  1. create a local interface
package com.test;
import javax.ejb.Local;

@Local
public interface TestEJBLocal {
 public void testMethod();
}

  1. Create a Remote interface


package com.test;
import javax.ejb.Remote;

@Remote
public interface TestEJBRemote {
      public void testMethod();
}

We can even create Remote and Local interface in single class by adding two annotations
@Remote
@Local

  1. Create a Bean class

package com.test;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class TestEJB
 */
@Stateless(mappedName = "ejb/TestEJB")
public class TestEJB implements TestEJBRemote, TestEJBLocal {

    /**
     * Default constructor.
     */
    public TestEJB() {
        // TODO Auto-generated constructor stub
    }

      public void testMethod() {
            System.out.println("Welcome to EJB 3.0");
           
      }

}
EJB are component based technology to call an EJB method we need to deploy in any application server.
Create a jar then deploy in application server.
Here I’m deploying in weblogic application server.

  1. Client class
public class TestEJBClient {
    public static void main(String [] args) {
        try {
            final Context context = getInitialContext();
            TestEJB testEJB = (TestEJB)context.lookup("ejb/TestEJB#com.test.TestEJB");
            //ejb/TestEJB is mapped name we provided in Bean then followed by the interface.
            testEJB.testMethod();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x connection details
        env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
        return new InitialContext( env );
    }
}

Just run the main class it prints Welcome message on console