发送ISO消息时使用ChannelAdaptor和QMUX存在问题。

huangapple go评论85阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;channel-adaptor name=&#39;test-channel&#39; 
class=&quot;org.jpos.q2.iso.ChannelAdaptor&quot; logger=&quot;Q2&quot;&gt;
&lt;channel class=&quot;org.jpos.iso.channel.ASCIIChannel&quot; logger=&quot;Q2&quot; realm=&quot;test-channel&quot;
packager=&quot;org.jpos.iso.packager.GenericPackager&quot;&gt;       
&lt;property name=&quot;packager-config&quot; value=&quot;src/main/resources/iso8583.xml&quot; /&gt;
&lt;property name=&quot;host&quot; value=&quot;127.0.0.1&quot; /&gt;
&lt;property name=&quot;port&quot; value=&quot;9090&quot; /&gt;
&lt;property name=&quot;connection-timeout&quot; value=&quot;15000&quot; /&gt; 
&lt;property name=&quot;timeout&quot; value=&quot;3000000&quot; /&gt;
&lt;property name=&quot;keep-alive&quot; value=&quot;true&quot; /&gt;  
&lt;/channel&gt;
&lt;!--    &lt;ignore-iso-exceptions&gt;yes&lt;/ignore-iso-exceptions&gt;--&gt;
&lt;in&gt;client-send&lt;/in&gt;
&lt;out&gt;client-receive&lt;/out&gt;
&lt;reconnect-delay&gt;10000&lt;/reconnect-delay&gt;
&lt;/channel-adaptor&gt;

20_mux.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;mux class=&quot;org.jpos.q2.iso.QMUX&quot; logger=&quot;Q2&quot; name=&quot;test-mux&quot;&gt;
&lt;in&gt;client-receive&lt;/in&gt;
&lt;out&gt;client-send&lt;/out&gt;
&lt;ready&gt;test-channel.ready&lt;/ready&gt;
&lt;unhandled&gt;myunhandledqueue&lt;/unhandled&gt;
&lt;key&gt;2 7&lt;/key&gt; 
&lt;/mux&gt;

JPos Client code

Q2 q2 = new Q2(&quot;src/main/deploy/&quot;);
q2.start();
QMUX mux = (QMUX)NameRegistrar.getIfExists(&quot;mux.test-mux&quot;);
if (mux != null &amp;&amp; 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(&quot;path_to_file_xml&quot;));
receivedIsoMsg.unpack(response.getBytes());
receivedIsoMsg.dump(System.out, &quot;&quot;);
}
}

Socket Server Code

ServerSocket serverSocket;
try {
System.out.println(&quot;Binding to port &quot; + SERVER_PORT + &quot;, please wait  ...&quot;);
serverSocket = new ServerSocket(SERVER_PORT);
System.out.println(&quot;Server started: &quot; + serverSocket);
System.out.println(&quot;Waiting for a client ...&quot;);
while (true) {
try {
Socket socket = serverSocket.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
String message = (String) dis.readLine();
System.out.println(&quot;Message Received: &quot; + message);                   
ISOMsg receivedIsoMsg = new ISOMsg();
receivedIsoMsg.setPackager(new GenericPackager(&quot;path_to_file_xml&quot;));
receivedIsoMsg.unpack(message.getBytes());
receivedIsoMsg.setMTI(&quot;0110&quot;);
receivedIsoMsg.set(39,&quot;00&quot;);
receivedIsoMsg.dump(System.out, &quot;&quot;);
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 ---
&lt;log realm=&quot;Q2.system&quot; at=&quot;2020-07-30T10:45:25.638&quot;&gt;
&lt;info&gt;
Q2 started, deployDir=/Users/lap/NetBeansProjects/JposTest/src/main/deploy, environment=default
&lt;/info&gt;
&lt;/log&gt;
&lt;log realm=&quot;Q2.system&quot; at=&quot;2020-07-30T10:45:26.160&quot; lifespan=&quot;507ms&quot;&gt;
&lt;version&gt;
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-----
&lt;/version&gt;
&lt;/log&gt;
&lt;log realm=&quot;Q2.system&quot; at=&quot;2020-07-30T10:45:26.168&quot; lifespan=&quot;5ms&quot;&gt;
&lt;info&gt;
deploy: /Users/lap/NetBeansProjects/JposTest/src/main/deploy/10_zp_channel.xml
&lt;/info&gt;
&lt;/log&gt;
&lt;log realm=&quot;Q2.system&quot; at=&quot;2020-07-30T10:45:26.199&quot; lifespan=&quot;30ms&quot;&gt;
&lt;info&gt;
deploy: /Users/lap/NetBeansProjects/JposTest/src/main/deploy/20_zp_qmux.xml
&lt;/info&gt;
&lt;/log&gt;
&lt;log realm=&quot;Q2.system&quot; at=&quot;2020-07-30T10:45:26.207&quot; lifespan=&quot;8ms&quot;&gt;
&lt;info&gt;
deploy: /Users/lap/NetBeansProjects/JposTest/src/main/deploy/99_sysmon.xml
&lt;/info&gt;
&lt;/log&gt;
&lt;log realm=&quot;org.jpos.q2.qbean.SystemMonitor&quot; at=&quot;2020-07-30T10:45:26.225&quot;&gt;
&lt;info&gt;
Starting SystemMonitor
&lt;/info&gt;
&lt;/log&gt;
&lt;log realm=&quot;org.jpos.q2.qbean.SystemMonitor&quot; at=&quot;2020-07-30T10:45:26.225&quot;&gt;
&lt;info&gt;
......
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
&lt;/info&gt;
&lt;/log&gt;
&lt;log realm=&quot;test-channel/127.0.0.1:9090&quot; at=&quot;2020-07-30T10:45:36.309&quot; lifespan=&quot;10084ms&quot;&gt;
&lt;connect&gt;
Try 0 127.0.0.1:9090
&lt;/connect&gt;
&lt;/log&gt;
&lt;log realm=&quot;test-channel/127.0.0.1:9090&quot; at=&quot;2020-07-30T10:45:55.579&quot; lifespan=&quot;1ms&quot;&gt;
&lt;send&gt;
&lt;isomsg direction=&quot;outgoing&quot;&gt;
&lt;!-- org.jpos.iso.packager.GenericPackager[src/main/resources/iso8583.xml] --&gt;
&lt;field id=&quot;0&quot; value=&quot;0100&quot;/&gt;
&lt;field id=&quot;2&quot; value=&quot;123456&quot;/&gt;
&lt;field id=&quot;3&quot; value=&quot;000010&quot;/&gt;
&lt;field id=&quot;4&quot; value=&quot;1500&quot;/&gt;
&lt;field id=&quot;7&quot; value=&quot;1206041200&quot;/&gt;
&lt;field id=&quot;11&quot; value=&quot;000001&quot;/&gt;
&lt;field id=&quot;41&quot; value=&quot;12340001&quot;/&gt;
&lt;field id=&quot;49&quot; value=&quot;840&quot;/&gt;
&lt;/isomsg&gt;
&lt;/send&gt;
&lt;/log&gt;

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
&lt;isomsg&gt;
&lt;!-- org.jpos.iso.packager.GenericPackager --&gt;
&lt;field id=&quot;0&quot; value=&quot;0110&quot;/&gt;
&lt;field id=&quot;8&quot; value=&quot;80000612&quot;/&gt;
&lt;field id=&quot;18&quot; value=&quot;3456&quot;/&gt;
&lt;field id=&quot;19&quot; value=&quot;000&quot;/&gt;
&lt;field id=&quot;20&quot; value=&quot;010&quot;/&gt;
&lt;field id=&quot;23&quot; value=&quot;000&quot;/&gt;
&lt;field id=&quot;27&quot; value=&quot;0&quot;/&gt;
&lt;field id=&quot;39&quot; value=&quot;00&quot;/&gt;
&lt;field id=&quot;57&quot; value=&quot;&quot;/&gt;
&lt;/isomsg&gt;

答案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.

huangapple
  • 本文由 发表于 2020年7月30日 12:37:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63166277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定