我的问题是我是 jpos 的新手,我想测试我的应用程序,我的应用程序包括一个服务器 A,它在服务器 B 和客户端(请求者)之间链接所以我必须实现一个客户端来向服务器 A 发送消息,然后将消息转发到服务器B,然后等待服务器B的响应并将其转发给客户端,服务器A正在监听8001并将来自客户端的消息通过端口8000和主机地址127.0.0.1转发到服务器B,这是我的最终实现:
我的通用打包程序(basic.xml):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE isopackager SYSTEM "genericpackager.dtd">
<isopackager>
<isofield
id="0"
length="4"
name="MESSAGE TYPE INDICATOR"
class="org.jpos.iso.IFA_NUMERIC"/>
<isofield
id="1"
length="64"
name="BIT MAP"
class="org.jpos.iso.IFA_BITMAP"/>
<isofield
id="3"
length="6"
name="PROCESSING CODE"
class="org.jpos.iso.IFA_NUMERIC"/>
<isofield
id="4"
length="12"
name="AMOUNT, TRANSACTION"
class="org.jpos.iso.IFA_NUMERIC"/>
<isofield
id="7"
length="10"
name="TRANSMISSION DATE AND TIME"
class="org.jpos.iso.IFA_NUMERIC"/>
<isofield
id="11"
length="6"
name="SYSTEM TRACE AUDIT NUMBER"
class="org.jpos.iso.IFA_NUMERIC"/>
<isofield
id="44"
length="25"
name="ADITIONAL RESPONSE DATA"
class="org.jpos.iso.IFA_LLCHAR"/>
<isofield
id="105"
length="999"
name="RESERVED ISO USE"
class="org.jpos.iso.IFA_LLLCHAR"/>
</isopackager>
00_logger.xml:
-------------------------------------------------------
<logger name="Q2" class="org.jpos.q2.qbean.LoggerAdaptor">
<log-listener class="org.jpos.util.SimpleLogListener" />
<log-listener class="org.jpos.util.BufferedLogListener">
<property name="max-size" value="100" />
<property name="name" value="logger.Q2.buffered" />
</log-listener>
<log-listener class="org.jpos.util.DailyLogListener">
<property name="window" value="86400" /> <!-- optional, default one day -->
<!--needed-->
<property name="prefix" value="log/q2" />
<property name="suffix" value=".log"/> <!-- optional -->
<!--optional the default is "-yyyy-MM-dd" -->
<property name="date-format" value="-yyyy-MM-dd-HH"/>
<!--optional the default is gzip-->
<property name="compression-format" value="gzip"/>
</log-listener>
</logger>
-------------------------------------------------------------
10_server_A.xml :
--------------------------------------------------------------
<server class="org.jpos.q2.iso.QServer" logger="Q2" name="Server-A">
<attr name="port" type="java.lang.Integer">8001</attr>
<attr name="maxSessions" type="java.lang.Integer">20</attr>
<attr name="minSessions" type="java.lang.Integer">10</attr>
<channel name="Server-A-Channel" class="org.jpos.iso.channel.HEXChannel" logger="Q2"
packager="org.jpos.iso.packager.GenericPackager">
<property name="packager-config" value="basic.xml" />
</channel>
<request-listener class="serverFront.ServerListener" logger="Q2">
<property name="space" value="transient:default" />
<property name="queue" value="TransactionQueue" />
<property name="timeout" value="10000" />
</request-listener>
</server>
-----------------------------------------------------------
11_server_B.xml :
------------------------------------------------------------
<server class="org.jpos.q2.iso.QServer" logger="Q2" name="Server-B">
<attr name="port" type="java.lang.Integer">8000</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.HEXChannel" logger="Q2"
packager="org.jpos.iso.packager.GenericPackager">
<property name="packager-config" value="basic.xml" />
</channel>
<request-listener class="serverB.ServerBListener" logger="Q2">
<property name="space" value="transient:default" />
<property name="queue" value="TransactionQueue" />
<property name="timeout" value="10000" />
</request-listener>
</server>
--------------------------------------------------------------------------------
30_client_channel.xml:
-------------------------------------------------------------------------------
<channel-adaptor name='client-adaptor' class="org.jpos.q2.iso.ChannelAdaptor"
logger="Q2">
<channel class="org.jpos.iso.channel.HEXChannel" packager="org.jpos.iso.packager.GenericPackager"
logger="Q2">
<property name="packager-config" value="basic.xml" />
<!-- Server A Address -->
<property name="host" value="127.0.0.1" />
<property name="port" value="8001" />
</channel>
<in>client-send</in>
<out>client-receive</out>
<reconnect-delay>10000</reconnect-delay>
</channel-adaptor>
-----------------------------------------------------------------------------
32_server_A_forward_channel.xml :
-----------------------------------------------------------------------------
<channel-adaptor name='server-A-adaptor' class="org.jpos.q2.iso.ChannelAdaptor" logger="Q2">
<channel class="org.jpos.iso.channel.HEXChannel"
packager="org.jpos.iso.packager.GenericPackager" logger="Q2">
<property name="packager-config" value="basic.xml" />
<property name="host" value="127.0.0.1" />
<property name="port" value="8000" />
</channel>
<in>serverA-forward-send</in>
<out>serverA-forward-receive</out>
<reconnect-delay>10000</reconnect-delay>
</channel-adaptor>
----------------------------------------------------------------------------
40_client_mux.xml :
----------------------------------------------------------------------------
<mux class="org.jpos.q2.iso.QMUX" logger="Q2" name="client-mux">
<in>client-receive</in>
<out>client-send</out>
</mux>
---------------------------------------------------------------
41_server_A_forward_mux.xml:
---------------------------------------------------------------
<mux class="org.jpos.q2.iso.QMUX" logger="Q2" name="serverA-forwarder-mux">
<in>serverA-forward-receive</in>
<out>serverA-forward-send</out>
</mux>
-----------------------------------------------------------------
99_sysmon.xml:
----------------------------------------------------------------
<sysmon logger="Q2">
<attr name="sleepTime" type="java.lang.Long">3600000</attr>
<attr name="detailRequired" type="java.lang.Boolean">true</attr>
</sysmon>
------------------------------------------------------
The client for sending the message:
------------------------------------------------------
package requester;
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.q2.iso.QMUX;
import org.jpos.util.NameRegistrar;
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 msg=createReqMsg();
ISOMsg reply = mux.request(msg, 10 * 1000);
System.out.println(new String(reply.pack()));
} catch (ISOException e) {
e.printStackTrace();
}
}
public static ISOMsg createReqMsg() {
ISOMsg reqMsg = new ISOMsg();
reqMsg.setHeader("0066".getBytes());
try {
reqMsg.set(3, "201234");
reqMsg.set(4, "10000");
reqMsg.set(7, "110722180");
reqMsg.set(11, "123456");
reqMsg.set(44, "A5DFGR");
reqMsg.set(105, "ABCDEFGHIJ 1234567890");
} catch (ISOException e) {
e.printStackTrace();
}
return reqMsg;
}
}
------------------------------------------------------------------------
server B :
------------------------------------------------------------------------
package serverB;
import java.io.IOException;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOSource;
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;
}
}
-------------------------------------------------------------------------------
And Server A for forwarding:
-------------------------------------------------------------------------------
package serverFront;
import java.io.IOException;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOSource;
import org.jpos.iso.MUX;
import org.jpos.q2.Q2;
import org.jpos.util.NameRegistrar;
public class ServerAListener implements ISORequestListener {
public static void main(String[] args) {
Q2 q2 = new Q2();
q2.start();
}
@Override
public boolean process(ISOSource isoSrc, ISOMsg isoMsg) {
try {
if (isoMsg.getMTI().equals("0800")) {
ISOMsg reply = (ISOMsg) isoMsg.clone();
reply.setResponseMTI();
reply.set(39, "00");
isoSrc.send(reply);
return true;
}
// send request to server B
MUX mux = (MUX) NameRegistrar.getIfExists("mux.serverA-forwarder-mux");
ISOMsg reply = mux.request(isoMsg, 10 * 1000);
if (reply != null) {
System.out.println(new String(reply.pack()));
reply.set(125, "RESPONSE FROM SERVER A");
isoSrc.send(reply);
}
} catch (ISOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
请注意,没有规范,只是我想从客户端发送一条消息,并通过服务器 A 将其转发给 B,然后等待 B 的响应然后将其发送给客户端。