Today, i want to share about jPOS Client Asynchronous Request. When we look at MUX interface there are two overloading request(ISOMsg, timeout) and request(ISOMsg, timeout, ISORequestListener, handback) method for synchronous and asynchronous messaging.
usually, i’am using request(ISOMsg, timeout) method for sending request to server. But when i look at MUX source for fun, i realize that there are asynchronous way to send request. So in this post i will share about jPOS Client Asynchronous Request.
You can see the different with my old post about jPOS Client Receive Response to Specific Port. in this post about jPOS Client Asynchronous Request receive response with same port when we are sending request, even both of my post can be used for asynchronous request methods.
So, why we need this asynchronous request?? to answere these question, look at image below:

Now, are you getting my point? yes, we can parallel the process! we don’t need to waste 200 ms for waiting the response. because waiting is always boring! 😀
See the QBean bellow to sending request asynchronously.
[java]
package com.didikhari.web.id.client;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOResponseListener;
import org.jpos.iso.MUX;
import org.jpos.q2.QBeanSupport;
import org.jpos.q2.iso.QMUX;
import org.jpos.space.Space;
import org.jpos.space.SpaceFactory;
import org.jpos.util.NameRegistrar;
import org.jpos.util.NameRegistrar.NotFoundException;
public class JposClient extends QBeanSupport implements ISOResponseListener {
private Long timeout ;
private Space
@SuppressWarnings(“unchecked” )
@Override
protected void initService() throws Exception {
this.timeout = cfg .getLong(“timeout”);
this.responseMap = SpaceFactory.getSpace (cfg .get(“spaceName” ));
NameRegistrar. register(getName(), this);
}
/**
* send request asynchronous way
* @param handback
* @param muxName
* @return
*/
public void sendRequest(ISOMsg reqMsg, String handback, String muxName) {
try {
MUX mux = QMUX. getMUX(muxName);
mux.request( reqMsg, timeout, this, handback );
} catch (NotFoundException e ) {
e. printStackTrace();
} catch (ISOException e ) {
e.printStackTrace();
}
}
/**
* getting response
* @param handback
* @return
*/
public ISOMsg getResponse(String handback ){
return responseMap .in(handback , timeout );
}
@Override
public void responseReceived(ISOMsg resp, Object handBack) {
responseMap.out(String.valueOf( handBack), resp, timeout);
}
@Override
public void expired(Object handBack) {
System. out.println(“Request ” +handBack +” is timeout” );
}
}
[/java]
And this sniped code for deploy the QBean
[xml]
[/xml]
So, lets test the bean using this code:
[java]
package com.didikhari.web.id.test;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import org.jpos.iso.ISODate;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.q2.Q2;
import org.jpos.util.NameRegistrar;
import org.junit.Assert;
import org.junit.Before;
import com.didikhari.web.id.client.JposClient;
import com.didikhari.web.id.constant.Parameter;
public class Test {
@Before
public void setUp() {
Q2 scanner = new Q2();
scanner.start();
ISOUtil. sleep(2000L);
}
@org.junit. Test
public void sendRequest() throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
JposClient client = (JposClient) NameRegistrar.getIfExists("requester");
client.sendRequest(createReqMsg(), "Testing"+i , "client-mux");
ISOMsg response = client.getResponse("Testing" +i );
Assert. assertNotNull("response is not null", response);
}
}
});
t.start();
t.join();
}
private ISOMsg createReqMsg() {
ISOMsg m = new ISOMsg();
try {
m.setMTI( "0800");
m.set(11, ISOUtil. getRandomDigits(new Random(), 6, 6));
m.set(7,ISODate. getDateTime(new Date(), TimeZone. getTimeZone(Parameter.JAKARTA_TIMEZONE)));
m.set(70, "001");
} catch (ISOException e ) {
e.printStackTrace();
}
return m ;
}
}
[/java]
So, as you can see! We can sending and getting response with this three line
[java]
JposClient client = (JposClient) NameRegistrar.getIfExists( “requester”);
client.sendRequest(createReqMsg(), “Testing” +i , “client-mux” );
//TODO do another parallel process
ISOMsg response = client .getResponse(“Testing” +i );
[/java]
So, i think i can’t share my project source. But feel free to ask me from comment section bellow, or from contact page.
I think, thats all i can share about jPOS Client Asynchronous Request, hope this post can help someone.
Thanks for visiting my blog. and so… um… if you need help for finish your project about jPOS, i think i can help you by remote your computer/laptop for some donation to my blog. 😀
Leave a Reply