Learn JPOS Part 1

Hi Today I am moving to new company and as part of my work I need to learn switching framework that is JPOS. Today I am start to read the JPOS documentation and try to write some blog post as way to remember and learn.
In the first part I am try to create channel that send the ISO Message and accept ISO Message from listener, before we continue with the code we need to highlight some term from the JPOS framework

ISOChannel

from documentation ISOChannel is interface that encapsulate wire protocol detail to send and receive ISOMessage so we can send and receive ISOMessage via ISOChannel

ISOServer

Briefly ISOServer is listen in a given port and take care the message and passing control for processing using implementation of ISOChannel (ISOListener)

Note: from JPOS user guide this approach is not advisable we should be using Q2

Create service channel as JPOS Client

package com.ge.c1;

import com.ge.DelayFilter;
import org.jpos.iso.FilteredChannel;
import org.jpos.iso.ISOChannel;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.channel.ASCIIChannel;
import org.jpos.iso.packager.ISO87APackager;
import org.jpos.util.LogSource;
import org.jpos.util.Logger;
import org.jpos.util.SimpleLogListener;

import java.io.IOException;

/**
 * Created by ggpratama on 10/4/2015.
 */
public class JPOSClient {
    public static void main(String[] args){
        Logger logger = new Logger();
        logger.addListener(new SimpleLogListener(System.out));
        ISOChannel channel = new ASCIIChannel("localhost",2300,new ISO87APackager());
        ((LogSource) channel).setLogger(logger, "client-logger");
        try {
            channel.connect();
            ISOMsg m = new ISOMsg ();
            m.setMTI ("0800");
            m.set (3, "000000");
            m.set (41, "00000001");
            m.set (70, "301");
            channel.send(m);
            //Get incoming message
            ISOMsg incoming = channel.receive();
            System.out.println(incoming.pack());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

Create ISOServer to listen given port

package com.ge.s1;

import org.jpos.iso.*;
import org.jpos.iso.channel.ASCIIChannel;
import org.jpos.iso.packager.ISO87APackager;
import org.jpos.util.LogSource;
import org.jpos.util.Logger;
import org.jpos.util.SimpleLogListener;

import java.io.IOException;

/**
 * Created by ggpratama on 10/4/2015.
 */
public class JPOSServer implements ISORequestListener{
    public static void main(String[] args){
        Logger logger = new Logger();
        logger.addListener(new SimpleLogListener(System.out));
        ServerChannel sChannel = new ASCIIChannel("localhost",2300,new ISO87APackager());
        ((LogSource)sChannel).setLogger(logger,"server-channel-logger");
        ISOServer isoServer = new ISOServer(2300,sChannel,null);
        isoServer.setLogger(logger,"server-logger");
        //Asign ISOListener Process
        isoServer.addISORequestListener(new JPOSServer());
        new Thread(isoServer).start();


    }

    @Override
    public boolean process(ISOSource isoSource, ISOMsg isoMsg) {

        ISOMsg m = (ISOMsg) isoMsg.clone();
        try {
            m.setMTI("0801");
            m.set(999,"INCOMING FROM SERVER");
            isoSource.send(m);
        } catch (ISOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }
}

Using channel filter

JPOS provide us the channel filter to filter the message from the channel, using channel filter we can prevent the message to send or receive or to delay the message (either send or receive). Bellow I will create filter to delay message before send from the client

package com.ge;

import org.jpos.iso.ISOChannel;
import org.jpos.iso.ISOFilter;
import org.jpos.iso.ISOMsg;
import org.jpos.util.LogEvent;

/**
 * Created by ggpratama on 10/4/2015.
 */
public class DelayFilter implements ISOFilter{
    private int delay;
    public DelayFilter(){
        delay = 0;
    }
    public DelayFilter(int delay){
        this.delay = delay;
    }
    @Override
    public ISOMsg filter(ISOChannel isoChannel, ISOMsg isoMsg, LogEvent logEvent) throws VetoException {
        logEvent.addMessage ("<delay-filter delay=\""+delay+"\"/>");
        if (delay > 0) {
            try {
                Thread.sleep (delay);
            } catch (InterruptedException e) { }
        }
        return isoMsg;
    }
}

And we need change the client code to using FilteredChannel instead ISOChannel, so the client code become

package com.ge.c1;

import com.ge.DelayFilter;
import org.jpos.iso.FilteredChannel;
import org.jpos.iso.ISOChannel;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.channel.ASCIIChannel;
import org.jpos.iso.packager.ISO87APackager;
import org.jpos.util.LogSource;
import org.jpos.util.Logger;
import org.jpos.util.SimpleLogListener;

import java.io.IOException;

/**
 * Created by ggpratama on 10/4/2015.
 */
public class JPOSClient {
    public static void main(String[] args){
        Logger logger = new Logger();
        logger.addListener(new SimpleLogListener(System.out));
        //Change to FilteredChannel
        FilteredChannel channel = new ASCIIChannel("localhost",2300,new ISO87APackager());
        //Initialize DelayFilter to delay message in 5 second
        DelayFilter delayFilter = new DelayFilter(5000);
        //Assign the filter
        channel.addFilter(delayFilter);
        ((LogSource) channel).setLogger(logger, "client-logger");
        try {
            channel.connect();
            ISOMsg m = new ISOMsg ();
            m.setMTI ("0800");
            m.set (3, "000000");
            m.set (41, "00000001");
            m.set (70, "301");
            channel.send(m);
            //Get incoming message
            ISOMsg incoming = channel.receive();
            System.out.println(incoming.pack());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

POM.xml

At last bellow is my maven configuration

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ge.s1</groupId>
    <artifactId>JPOSClientApplication</artifactId>
    <version>1.0</version>
    <repositories>
        <repository>
            <id>oracle</id>
            <name>Oracle repo</name>
            <url>http://download.oracle.com/maven</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.jpos</groupId>
            <artifactId>jpos</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>
</project>

Hope this post can help anyone to getting started with JPOS and soon I will try to write another post about Q2.

happy coding

 
79
Kudos
 
79
Kudos

Now read this

Create your own callback in Java

Creating callback in Java need utilize the interface unlike the javascript there is quite a work to creating callback pattern in Java. using interface public interface WordIntf{ public void saysSomething(String salutation); } Create the... Continue →