英文:
Problem with sending iso message using ChannelAdaptor & QMUX
问题
我正在使用通道适配器和qmux将iso8583消息发送到套接字服务器。
我在NetBeans中编写了两个Maven项目,一个是Jpos客户端,一个是服务器。
服务器接受连接,但不接收消息(或客户端不发送消息),但当我取消客户端项目的进程时,服务器接收到消息。
如果我错误,请纠正我。
以下是我的配置文件(参考http://jpos.org/doc/proguide-draft.pdf):
10_channel.xml
<?xml version="1.0" encoding="UTF-8"?>
<channel-adaptor name='test-channel' class="org.jpos.q2.iso.ChannelAdaptor" logger="Q2">
<channel class="org.jpos.iso.channel.ASCIIChannel" logger="Q2" realm="test-channel"
packager="org.jpos.iso.packager.GenericPackager">
<property name="packager-config" value="src/main/resources/iso8583.xml" />
<property name="host" value="127.0.0.1" />
<property name="port" value="9090" />
<property name="connection-timeout" value="15000" />
<property name="timeout" value="3000000" />
<property name="keep-alive" value="true" />
</channel>
<!-- <ignore-iso-exceptions>yes</ignore-iso-exceptions>-->
<in>client-send</in>
<out>client-receive</out>
<reconnect-delay>10000</reconnect-delay>
</channel-adaptor>
20_mux.xml
<?xml version="1.0" encoding="UTF-8"?>
<mux class="org.jpos.q2.iso.QMUX" logger="Q2" name="test-mux">
<in>client-receive</in>
<out>client-send</out>
<ready>test-channel.ready</ready>
<unhandled>myunhandledqueue</unhandled>
<key>2 7</key>
</mux>
JPos客户端代码
Q2 q2 = new Q2("src/main/deploy/");
q2.start();
QMUX mux = (QMUX)NameRegistrar.getIfExists("mux.test-mux");
if (mux != null && mux.isConnected()) {
ISOMsg request = new IsoMessage().build(); //dump iso message
ISOMsg response = mux.request(request, REQUEST_TIMEOUT);
if (response != null) {
ISOMsg receivedIsoMsg = new ISOMsg();
receivedIsoMsg.setPackager(new GenericPackager("path_to_file_xml"));
receivedIsoMsg.unpack(response.getBytes());
receivedIsoMsg.dump(System.out, "");
}
}
套接字服务器代码
ServerSocket serverSocket;
try {
System.out.println("Binding to port " + SERVER_PORT + ", please wait ...");
serverSocket = new ServerSocket(SERVER_PORT);
System.out.println("Server started: " + serverSocket);
System.out.println("Waiting for a client ...");
while (true) {
try {
Socket socket = serverSocket.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
String message = (String) dis.readLine();
System.out.println("Message Received: " + message);
ISOMsg receivedIsoMsg = new ISOMsg();
receivedIsoMsg.setPackager(new GenericPackager("path_to_file_xml"));
receivedIsoMsg.unpack(message.getBytes());
receivedIsoMsg.setMTI("0110");
receivedIsoMsg.set(39,"00");
receivedIsoMsg.dump(System.out, "");
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//write object to Socket
dos.writeUTF(new String(receivedIsoMsg.pack()));
} catch (IOException | ISOException ex) {
System.err.println(ex);
}
}
} catch (IOException e1) {
try {
serverSocket.close();
} catch (IOException ex) {
System.err.println(ex);
}
}
跟踪日志
客户端:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ JposTest ---
<log realm="Q2.system" at="2020-07-30T10:45:25.638">
<info>
Q2 started, deployDir=/Users/lap/NetBeansProjects/JposTest/src/main/deploy, environment=default
</info>
</log>
...
<log realm="test-channel/127.0.0.1:9090" at="2020-07-30T10:45:55.579" lifespan="1ms">
<send>
<isomsg direction="outgoing">
<!-- org.jpos.iso.packager.GenericPackager[src/main/resources/iso8583.xml] -->
<field id="0" value="0100"/>
<field id="2" value="123456"/>
<field id="3" value="000010"/>
<field id="4" value="1500"/>
<field id="7" value="1206041200"/>
<field id="11" value="000001"/>
<field id="41" value="12340001"/>
<field id="49" value="840"/>
</isomsg>
</send>
</log>
服务器:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ AppTest ---
Binding to port 9090, please wait ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=9090]
Waiting for a client ...
Client accepted: Socket[addr=/127.0.0.1,port=58902,localport=9090]
=> 客户端无法发送消息
但是如果我取消客户端项目的进程,服务器会接收消息并打印日志:
...
Message Received: 00730100722000000080800006123456000010000000001500120604120000000112340001840
<isomsg>
<!-- org.jpos.iso.packager.GenericPackager -->
<field id="0" value="0110"/>
<field id="8" value="80000612"/>
<field id="18" value="3456"/>
<field id="19" value="000"/>
<field id="20" value="010"/>
<field id="23" value="000"/>
<field id="27" value="0"/>
<field id="39" value="00"/>
<field id="57" value=""/>
</isomsg>
请检查配置和代码,确保客户端正确发送消息并且服务器能够正确接收消息。如果您有更多问题,请提出具体问题,我将尽力回答。
英文:
I am using a channel adaptor and qmux to send iso8583 message to a socket server.
I write 2 maven project Jpos client & server in netbeans.
The server accepts the connection but doesnt receive the message (or the client doesnt send message) but when I cancel process of the client project, the server receives
Plz correct me if i wrong.
Following are my configuration files (Refered to http://jpos.org/doc/proguide-draft.pdf)
10_channel.xml
<?xml version="1.0" encoding="UTF-8"?>
<channel-adaptor name='test-channel'
class="org.jpos.q2.iso.ChannelAdaptor" logger="Q2">
<channel class="org.jpos.iso.channel.ASCIIChannel" logger="Q2" realm="test-channel"
packager="org.jpos.iso.packager.GenericPackager">
<property name="packager-config" value="src/main/resources/iso8583.xml" />
<property name="host" value="127.0.0.1" />
<property name="port" value="9090" />
<property name="connection-timeout" value="15000" />
<property name="timeout" value="3000000" />
<property name="keep-alive" value="true" />
</channel>
<!-- <ignore-iso-exceptions>yes</ignore-iso-exceptions>-->
<in>client-send</in>
<out>client-receive</out>
<reconnect-delay>10000</reconnect-delay>
</channel-adaptor>
20_mux.xml
<?xml version="1.0" encoding="UTF-8"?>
<mux class="org.jpos.q2.iso.QMUX" logger="Q2" name="test-mux">
<in>client-receive</in>
<out>client-send</out>
<ready>test-channel.ready</ready>
<unhandled>myunhandledqueue</unhandled>
<key>2 7</key>
</mux>
JPos Client code
Q2 q2 = new Q2("src/main/deploy/");
q2.start();
QMUX mux = (QMUX)NameRegistrar.getIfExists("mux.test-mux");
if (mux != null && mux.isConnected()) {
ISOMsg request = new IsoMessage().build(); //dump iso message
ISOMsg response = mux.request(request, REQUEST_TIMEOUT);
if (response != null) {
ISOMsg receivedIsoMsg = new ISOMsg();
receivedIsoMsg.setPackager(new GenericPackager("path_to_file_xml"));
receivedIsoMsg.unpack(response.getBytes());
receivedIsoMsg.dump(System.out, "");
}
}
Socket Server Code
ServerSocket serverSocket;
try {
System.out.println("Binding to port " + SERVER_PORT + ", please wait ...");
serverSocket = new ServerSocket(SERVER_PORT);
System.out.println("Server started: " + serverSocket);
System.out.println("Waiting for a client ...");
while (true) {
try {
Socket socket = serverSocket.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
String message = (String) dis.readLine();
System.out.println("Message Received: " + message);
ISOMsg receivedIsoMsg = new ISOMsg();
receivedIsoMsg.setPackager(new GenericPackager("path_to_file_xml"));
receivedIsoMsg.unpack(message.getBytes());
receivedIsoMsg.setMTI("0110");
receivedIsoMsg.set(39,"00");
receivedIsoMsg.dump(System.out, "");
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//write object to Socket
dos.writeUTF(new String(receivedIsoMsg.pack()));
} catch (IOException | ISOException ex) {
System.err.println(ex);
}
}
} catch (IOException e1) {
try {
serverSocket.close();
} catch (IOException ex) {
System.err.println(ex);
}
}
Trace log
Client:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ JposTest ---
<log realm="Q2.system" at="2020-07-30T10:45:25.638">
<info>
Q2 started, deployDir=/Users/lap/NetBeansProjects/JposTest/src/main/deploy, environment=default
</info>
</log>
<log realm="Q2.system" at="2020-07-30T10:45:26.160" lifespan="507ms">
<version>
jPOS 2.1.3 master/95b8dce (2019-06-16 15:16:57 ART)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
jPOS Community Edition, licensed under GNU AGPL v3.0.
This software is probably not suitable for commercial use.
Please see http://jpos.org/license for details.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)
iQEcBAEBAgAGBQJMolHDAAoJEOQyeO71nYtFv74H/3OgehDGEy1VXp2U3/GcAobg
HH2eZjPUz53r38ARPiU3pzm9LwDa3WZgJJaa/b9VrJwKvbPwe9+0kY3gScDE1skT
ladHt+KHHmGQArEutkzHlpZa73RbroFEIa1qmN6MaDEHGoxZqDh0Sv2cpvOaVYGO
St8ZaddLBPC17bSjAPWo9sWbvL7FgPFOHhnPmbeux8SLtnfWxXWsgo5hLBanKmO1
1z+I/w/6DL6ZYZU6bAJUk+eyVVImJqw0x3IEElI07Nh9MC6BA4iJ77ejobj8HI2r
q9ulRPEqH9NR79619lNKVUkE206dVlXo7xHmJS1QZy5v/GT66xBxyDVfTduPFXk=
=oP+v
-----END PGP SIGNATURE-----
</version>
</log>
<log realm="Q2.system" at="2020-07-30T10:45:26.168" lifespan="5ms">
<info>
deploy: /Users/lap/NetBeansProjects/JposTest/src/main/deploy/10_zp_channel.xml
</info>
</log>
<log realm="Q2.system" at="2020-07-30T10:45:26.199" lifespan="30ms">
<info>
deploy: /Users/lap/NetBeansProjects/JposTest/src/main/deploy/20_zp_qmux.xml
</info>
</log>
<log realm="Q2.system" at="2020-07-30T10:45:26.207" lifespan="8ms">
<info>
deploy: /Users/lap/NetBeansProjects/JposTest/src/main/deploy/99_sysmon.xml
</info>
</log>
<log realm="org.jpos.q2.qbean.SystemMonitor" at="2020-07-30T10:45:26.225">
<info>
Starting SystemMonitor
</info>
</log>
<log realm="org.jpos.q2.qbean.SystemMonitor" at="2020-07-30T10:45:26.225">
<info>
......
thread count: 10
peak threads: 10
user threads: 7
Thread[Reference Handler,10,system]
Thread[Finalizer,8,system]
Thread[Signal Dispatcher,9,system]
Thread[main,5,main]
Thread[pool-1-thread-1,5,main]
Thread[Q2-dd666708-34a1-46dc-9a10-4df253d9249a,5,main]
Thread[Thread-1,5,main]
Thread[channel-sender-client-send,5,main]
Thread[channel-receiver-client-receive,5,main]
Thread[SystemMonitor,5,main]
name-registrar:
tspace:default: org.jpos.space.TSpace
key-count: 0
gcinfo: 0,0
Q2: org.jpos.q2.Q2
test-channel: org.jpos.q2.iso.ChannelAdaptor
tx=0, rx=0, connects=0, last=0
logger.Q2: org.jpos.util.Logger
channel.test-channel: org.jpos.iso.channel.ASCIIChannel
mux.test-mux: org.jpos.q2.iso.QMUX
tx=0, rx=0, tx_expired=0, tx_pending=0, rx_expired=0, rx_pending=0, rx_unhandled=0, rx_forwarded=0, connected=false, last=0
logger.: org.jpos.util.Logger
</info>
</log>
<log realm="test-channel/127.0.0.1:9090" at="2020-07-30T10:45:36.309" lifespan="10084ms">
<connect>
Try 0 127.0.0.1:9090
</connect>
</log>
<log realm="test-channel/127.0.0.1:9090" at="2020-07-30T10:45:55.579" lifespan="1ms">
<send>
<isomsg direction="outgoing">
<!-- org.jpos.iso.packager.GenericPackager[src/main/resources/iso8583.xml] -->
<field id="0" value="0100"/>
<field id="2" value="123456"/>
<field id="3" value="000010"/>
<field id="4" value="1500"/>
<field id="7" value="1206041200"/>
<field id="11" value="000001"/>
<field id="41" value="12340001"/>
<field id="49" value="840"/>
</isomsg>
</send>
</log>
Server:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ AppTest ---
Binding to port 9090, please wait ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=9090]
Waiting for a client ...
Client accepted: Socket[addr=/127.0.0.1,port=58902,localport=9090]
=> the Client cannot send message
But if I cancel process of client project, the Server receives message & print log:
Message Received: 00730100722000000080800006123456000010000000001500120604120000000112340001840
<isomsg>
<!-- org.jpos.iso.packager.GenericPackager -->
<field id="0" value="0110"/>
<field id="8" value="80000612"/>
<field id="18" value="3456"/>
<field id="19" value="000"/>
<field id="20" value="010"/>
<field id="23" value="000"/>
<field id="27" value="0"/>
<field id="39" value="00"/>
<field id="57" value=""/>
</isomsg>
答案1
得分: 2
问题在于您正在尝试使用readLine
接收消息,而ISO消息不是以EOL结尾的字符串。
我认为发生的情况是,当客户端断开连接时,readLine
方法会返回,因为输入流已关闭。
最好在服务器上使用ISOServer
,或者更好地使用完整的q2
来实现它。
否则,您应该首先读取消息的长度,然后从输入流中读取相同数量的字节,而不是等待可能包含在消息本身中的换行符。
您可以在这里查看前两个jpos教程 http://www.jpos.org/tutorials,以了解如何配置一个带有请求监听器的服务器来处理请求。
英文:
Problem is you are trying to receive the message using readLine
and iso messages are not strings ended with an EOL.
I believe what's happening is that when the client disconnects the readLine
method returns, because the input stream is closed.
It would be better to use ISOServer
on your server, or even better a full q2
to implement it.
Otherwise you should first read the message length and then read that amount of bytes from the input stream, instead of waiting for a new line character that may or may not be part of the message itself
You can follow the first two jpos tutorials here http://www.jpos.org/tutorials, to get an idea on how to configure a Server with a request listener to process requests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论