jPOS Server Forward Message Simulator

Hi, Today i want to share my code about jPOS Server Forward Message Simulator. The simulator flow is described on my old post here: JPos Server Forward Request Message to Another Server. My old post is talk about the “front end” server that receive request from client and forward to “back end” server, so in this post we will talk about the client and backend server.

jPOS Server Forward Message Simulator
jPOS Server Forward Message Simulator

I have refactor my code from my old post, so maybe you’ll find different term.
We start with the backend server, or on my old post is Server B, first you need to create file named 11_server_B.xml containing this code.

<server class="org.jpos.q2.iso.QServer" logger="Q2" name="Server-B">
    <attr name="port" type="java.lang.Integer">12201</attr>
    <attr name="maxSessions" type="java.lang.Integer">100</attr>	
    <attr name="minSessions" type="java.lang.Integer">10</attr>

    <channel name="Server-B-Channel" class="org.jpos.iso.channel.NACChannel" logger="Q2"
             packager="com.didikhari.packager.MyPackager"
             header="000000000000000000000000">
    </channel>

    <request-listener class="com.didikhari.controller.ServerBListener" logger="Q2">
        <property name="space" value="transient:default" />
        <property name="queue" value="TransactionQueue" />
        <property name="timeout" value="10000" />
    </request-listener>
	
</server>


And the Listener class is:

package com.didikhari.controller;

import java.io.IOException;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOSource;

/**
 * Server B Listener<br/>
 * Listener for Server A Request (Back end Server)
 * @author Didik Hari
 *
 */
public class ServerBListener implements ISORequestListener {

	@Override
	public boolean process(ISOSource requester, ISOMsg reqMsg) {
		try {
			ISOMsg respMsg = (ISOMsg) reqMsg.clone();
			respMsg.set(124, "HI CLIENT, THIS IS DATA FROM ME! B");
			respMsg.set(39, "00");
			respMsg.setResponseMTI();
			requester.send(respMsg);
		} catch (ISOException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

}



The ServerBListener is used for receive request from server A, and send response back.

Thats it the backend server, you can find about configuration front end server on my old post JPos Server Forward Request Message to Another Server

And the last thing we need to do is create a requester class, or called “Client”. For client implementation, we need to create two file configuration named 30_client_channel.xml and 40_client_mux.xml.
First, create 30_client_channel.xml file and give this code:

<channel-adaptor name='client-adaptor' class="org.jpos.q2.iso.ChannelAdaptor" logger="Q2">
	
	<channel class="org.jpos.iso.channel.NACChannel" header="000000000000000000000000"
		packager="com.didikhari.packager.MyPackager" logger="Q2">

		<!-- Server A Address -->
		<property name="host" value="127.0.0.1" />
		<property name="port" value="15500" />
	</channel>
	
	<in>client-send</in>
	<out>client-receive</out>
	<reconnect-delay>10000</reconnect-delay>
</channel-adaptor>

and the 40_client_mux.xml is:

<mux class="org.jpos.q2.iso.QMUX" logger="Q2" name="client-mux">
	<in>client-receive</in>
	<out>client-send</out>
</mux>

Client channel adaptor will send request message to Server A adress and wait response on it.

But How to start the jPOS Server Forward Message Simulator?? we need to create a class that will call Q2.start method, so the jPOS Library will deploy the configuration we have made.
package com.didikhari.requester;

import java.util.Date;

import org.jpos.iso.ISODate;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.iso.MUX;
import org.jpos.q2.Q2;
import org.jpos.util.NameRegistrar;

/**
 * Requester
 * @author Didik Hari
 *
 */
public class Client {
	
	public static void startQ2(){
		Q2 q2 = new Q2();
		q2.start();
		System.out.println("Starting Deployment");
	}
	
	public static void main(String[] args) {
		startQ2();
		// give some delay before sent req, cz q2 deployment is async
		ISOUtil.sleep(5000);
		
		// Process send and receive data
		try {
			MUX mux = (MUX) NameRegistrar.getIfExists("mux.client-mux");
			ISOMsg reply = mux.request(createReqMsg(), 10 * 1000);
			System.out.println(new String(reply.pack()));
		} catch (ISOException e) {
			e.printStackTrace();
		}
	}
	
	public static ISOMsg createReqMsg(){
		ISOMsg reqMsg = new ISOMsg();
		reqMsg.setHeader("ISO011000017".getBytes());
		try {
			reqMsg.setMTI("0200");
			reqMsg.set(3, "530020");
			reqMsg.set(7, ISODate.getDateTime(new Date()));
			reqMsg.set(11, String.valueOf(System.currentTimeMillis() % 1000000));
			reqMsg.set(12, ISODate.getTime(new Date()));
			reqMsg.set(13, ISODate.getDate(new Date()));
			reqMsg.set(14, "1502");
			reqMsg.set(17, ISODate.getDate(new Date()));
			reqMsg.set(18, "6011");
			reqMsg.set(35, "6011343456sdgfd");
			reqMsg.set(37, "123456789012");
			reqMsg.set(41, "1234567890123456");
			reqMsg.set(124, "GIVE ME DATA FROM SERVER B");
		} catch (ISOException e) {
			e.printStackTrace();
		}
		return reqMsg;
	}
}



DONE!!
it’s all the jPOS Server Forward Message Simulator.
You can download my eclipse jPOS Server Forward Message Simulator project by click this link bellow:

Please disable popup blocker for view the link, after you click the link below it will open new tab for downloading the files.


Hope this post can help somebody, thanks for comming in my blog.
Regards.

15 comments

  1. Hi Didik H;
    Thank you for the post. I have been trying to run this it is failing getting can’t load com.mandiri.soa.ubp.UBPPackager. I was trying to look where i can modify and my my own packager because i don’t find yours, i can’t get the location for that. The post look very helpful for once i managed to run it.

  2. Good day Didik,
    I am not sure if I am doing this right. I downloaded your files to test. Remapped the JAR files to the ones I have for jPos but I cannot compile and run your client nor the servers.
    Am I missing a step somewhere?
    I have jPos-2.0.4.

  3. hi . i get error
    Exception in thread “main” java.lang.NoClassDefFoundError: org/jdom/Content
    at Client.startQ2(Client.java:10)
    at Client.main(Client.java:16)
    Caused by: java.lang.ClassNotFoundException: org.jdom.Content
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    why ?

  4. Konsep ISO saya sudah paham nih mas, cuma konsep pengembangan JPos masih buta. Harus belajar dari mana dulu untuk nguasain JPos. kalo ada link tutorial yang bisa dipelajari mohon dishare mas.

    1. Hallo mas, kalau saya dulu coba pelajari dari programmer guide nya mas, ada bahan pembelajaran dikasih di kantor. lumayan bisa lebih paham konsep dasarnya, dan cara custom channel, packager dan QBean. selebihnya coba untuk pelajari code nya, mulai dai class Q2, sampai ke configurable class yang lain seperti QMUX, QServer, Space, NameRegistrar, dll.

Leave a Reply

Your email address will not be published. Required fields are marked *