jPOS Client Receive Response to Specific Port

Today, i want to share about jPOS Client Receive Response to Specific Port. In my current project integration between SOA and DSP, the basic requirment is, DSP will send request message(not define the request port) to SOA (as Server, SOA listen to spesific port) and DSP will receive the SOA response to the specific port(may different with request port). here the ilustration:

jPOS client receive response to the spesific port
jPOS client receive response to the spesific port

The main idea is using Q2, and create standard jpos MUX to send request (without waiting the response), and create jpos org.jpos.q2.iso.QServer (that listen to specific port) for receive the response. and the question is, how we manage the message thread?? how match the ISO response to their request message? to achieve these question, we will use jpos space.
from jpos programmer guide,

You can think about jPOS’s Space component as being like a Map where its entries are lists of objects and its operations are fully synchronized

for achieve the message thread, we need to put ISO message to the jPOS’s Space with unique key (for example using bit combination 41 and 11).

First thing we need to do is create client adaptor, 15_clientsimulator_channel.xml


[xml]


jpos-client-send
jpos-client-receive
10000

[/xml]

and then, create MUX 20_clientsimulator_mux.xml

[xml]

jpos-client-receive
jpos-client-send

[/xml]

we will create channelManager 25_jposclient_channel_manager.xml, this class is extends QBeanSupport used for managing the channel(channel config, sending request, getting response from space)

[xml]


[/xml]

[java]
public class ChannelManager extends QBeanSupport {
private long MAX_TIME_OUT;
private MUX mux;

@Override
protected void initService() throws ISOException {
log.info(“initializing ChannelManager Service”);
try {
mux = (MUX) NameRegistrar.get(“mux.” + cfg.get(“mux”));
MAX_TIME_OUT = cfg.getLong(“timeout”);
NameRegistrar.register(“manager”, this);
} catch (NameRegistrar.NotFoundException e) {
log.error(“Error in initializing service :” + e.getMessage());
}

}

public ISOMsg sendMsg(ISOMsg m) throws Exception {
return sendMsg(m, mux, MAX_TIME_OUT);
}

@SuppressWarnings({“unchecked” })
private ISOMsg sendMsg(ISOMsg msg, MUX mux, long time) throws Exception {

if (mux != null) {
long start = System.currentTimeMillis();
mux.request(msg, 1);
String msgId = msg.getString(11);
Space space = SpaceFactory.getSpace(“tspace:mySpace”);
ISOMsg responseInSpace = space.in(msgId, 10000L);
long duration = System.currentTimeMillis() – start;
log.info(“Response time (ms):” + duration);
return responseInSpace;
}
return null;
}
}
[/java]

space.in(msgId, 10000L) will Wait a maximum of 10000 milliseconds for a given entry; otherwise, return null.

Before we create the requester class, we need to prepare jPOS QServer for listen the response and put to the Space

[xml]

12261
50
10



[/xml]

[java]
public class ResponseListener implements ISORequestListener, Configurable {

private Space sp;

@Override
public boolean process(ISOSource source, ISOMsg msg) {
sp.out(msg.getString(11), msg);
return true;
}

@SuppressWarnings(“unchecked”)
@Override
public void setConfiguration(Configuration conf)
throws ConfigurationException {
sp = SpaceFactory.getSpace(conf.get(“space”));
}

}
[/java]

sp.out(msg.getString(11), msg) will put the response message to the space and give it key with bit 11 from the message.

and the last thing we have to do is create the requester class

[java]
public class JPosClient {

static void start() {
Q2 q2 = new Q2();
q2.start();
}

public static void main(String[] args) throws ISOException {
start();
ISOUtil.sleep(2000);
new Thread(new Exec()).start();
}

static class Exec implements Runnable {

ChannelManager channelManager;

Exec() throws ISOException {
try {
channelManager = ((ChannelManager) NameRegistrar.get(“manager”));
} catch (NameRegistrar.NotFoundException e) {
LogEvent evt = channelManager.getLog().createError();
evt.addMessage(e);
evt.addMessage(NameRegistrar.getInstance());
Logger.log(evt);
} catch (Throwable t) {
channelManager.getLog().error(t);
}

}

private ISOMsg createHandshakeISOMsg() throws ISOException {
ISOMsg m = new ISOMsg();
m.setHeader(“ISO006000050”.getBytes());
m.setMTI(“0800”);
m.set(7, ISODate.getDateTime(new Date()));
m.set(11, String.valueOf(System.currentTimeMillis() % 1000000));
m.set(32, “00001603307”);
m.set(41, “T1603307”);
m.set(70, “301”);
return m;
}

private void sendHandShake() throws Exception {
try {
ISOMsg response = channelManager.sendMsg(createHandshakeISOMsg());
channelManager.getLog().info(new String(response.pack()));
} catch (ISOException e1) {
channelManager.getLog().error(
“ISOException :” + e1.getMessage());
} catch (Exception e) {
channelManager.getLog().error(“Exception :” + e.getMessage());
}

}

@Override
public void run() {
while (true) {
// ISOUtil.sleep(2000);
try {
sendHandShake();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
[/java]

it’s All post about jPOS Client Receive Response to Specific Port, hope this post can help you. please leave comment below.

15 responses to “jPOS Client Receive Response to Specific Port”

  1. Dany Avatar
    Dany

    Hi Didik.

    i’m working with jpos and ISO8583 about 4 years, and every day learn new concepts and ways. You help me very Much with concepts and procedures.

    I use mux, channels via xml, and ever have the same question, when is fired a timeout?

    try{
    MUX mux = (MUX) NameRegistrar.get(“mux.movistar_mux”);
    ISOMsg response = mux.request(m, 15000);
    if(response != null){

    }else{
    // HERE???????????
    }
    }catch(Exception e){
    // OR, HERE???????????
    }

    Please, hep me.

    Thank you vey much.

    1. Didik H Avatar

      Hi Dany,

      Thank you for visiting my blog. I still learn too, so many things we can explore with jPOS.

      Actually, mux and channel comunicate using space. Thats why we set in out configuration on mux and channel.
      Mux will look up in the space until 1500ms, when nothing found there, mux will got null. So, i think the respose = null, not on the catch block.

  2. Mubin Avatar
    Mubin

    Hi Didik,

    Thanks for the great posting. I guess you forget to add ClientRequestListener class in the blog.

    1. Didik H Avatar

      Thank you Mubin, hmmm i didn’t check it yet.. but thanks for your feedback, ClientRequestListener is just class that implement ISORequestListener interface.

  3. demii Avatar
    demii

    Saya mencoba Jpoc client ini, awalnya Exec() dieksekusi dan HandShake dilakukan tiap 2 detik. lalu program saya hentikan dan saya coba jalankan ulang, namun Exec() tidak dijalankan hanya sampai ipserver:port
    saya coba berulang kali namun Exec() tidak pernah dieksekusi lagi.

    1. Didik H Avatar

      Hi mas Demii,

      Apakah ada error yang muncul?

    2. demii Avatar
      demii

      tidak ada.. program terus berjalan namun log berhenti sampai conect ipserver:port
      , ditunggu lama looping handshake tidak jalan.
      awalnya looping handshake jalan, namun program dihentikan. saat dicoba di run kembali tidak pernah jalan lagi.

  4. Didik H Avatar

    bagian kode yang di update ada nggak mas?

    1. demii Avatar
      demii

      tidak ada. tidak ada yang saya edit hanya dijalankan –> di hentikan –> dijalankan ulang.

      1. Didik H Avatar

        Mas Demii punya class com.didikhari.packager.MyPackager ?? kalau tidak tolong semua dirubah menjadi org.jpos.iso.packager.BASE24Packager bawaan jpos

        kemudian tolong update 20_clientsimulator_mux.xml menjadi:

        
        	jpos-client-receive
        	jpos-client-send
        
        
    2. demii Avatar
      demii

      saya menggunakan
      channel class=”org.jpos.iso.channel.ASCIIChannel” logger=”Q2 packager=”org.jpos.iso.packager.GenericPackager”

  5. Rofi Avatar
    Rofi

    Tutorialnya sangat membantu. tapi saya mengalami sedikit masalah, ketika saya jalankan project client dan server, result nya terus melakukan perulangan. Kira2 solusinya gimana ya?

    1. Rofi Avatar
      Rofi

      sudah terpecahkan. hehe

  6. shahrad Avatar
    shahrad

    Hi didik

    i want get response connection that i used to send request .
    what should i do ?

    1. didikhari Avatar
      didikhari

      Hi shahrad,

      normally mux.request() will return whether null or response message.

Leave a Reply

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