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:

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]
[/xml]
and then, create MUX 20_clientsimulator_mux.xml
[xml]
[/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
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]
[/xml]
[java]
public class ResponseListener implements ISORequestListener, Configurable {
private Space
@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.
Leave a Reply