Summary of the invention
The objective of the invention is the message communicating mechanism transmitted for based on a kind of order of design between the WEB application system of Java EE platform and the remote equipment and response.The various command that it can effectively send the WEB application system is transmitted to remote equipment, waits for and transmit the response message that remote equipment is beamed back.Its outstanding feature is the forwards mechanism through the JMS formation, and the WEB application system is decomposed into WEB user interactions layer and two Distributed Application relatively independent, low coupling of remote equipment alternation of bed, has realized separating of service logic and presentation layer.
The objective of the invention is to realize through following technical scheme.
A kind of distributed WEB of the present invention is used and the remote equipment information communication method, comprises step:
1) the JMS formation of initialization WEB application system, utility command manager, GPRS service;
2) user sends the operational order to remote equipment through WEB user interactions layer, and initiation session process, WEB alternation of bed adopt synchronous mode to order through the JMS formation and be forwarded to the remote equipment alternation of bed; Simultaneously, set up the interim formation of JMS, transmit leg blocks thread, wait-for-response message;
3) after the remote equipment alternation of bed is received the order of JMS message of forwarding, according to the communication protocol of remote equipment agreement, command object is resolved to byte stream, and sends to remote equipment through public network;
4) the remote equipment alternation of bed is received response message at the appointed time, and byte stream is resolved to the response message object, and the corresponding order of coupling is beamed back WEB user interactions layer, conversation end through the interim formation of JMS with response from command queue; If, then give time-out error response message of WEB user interactions layer loopback, end session above the response that the fixed time is not received remote equipment yet.
In the step 2; Said user specifically comprises through the operational order that WEB user interactions layer sends remote equipment: the WEB application system is obtained the JMS message object through data center from the JMS formation; Therefrom extract command object COMMAND, preservation therein is used to respond the interim queuing message of the JMS that returns, and command object COMMAND is put into the order pond; Resolve to byte stream according to communication protocol, the network port socket through data center sends to remote equipment.
In the step 4; Saidly through the interim formation of JMS WEB user interactions layer is beamed back in response and be specially: the response message byte stream through data center's receiving remote equipment returns, it is resolved to response message object RESPONSE, in the order pond, search its corresponding command object COMMAND; As find the command object that is complementary; Therefrom obtain the interim queuing message of JMS, response message RESPONSE is returned to the WEB application system through the interim formation of this JMS, further present to the user.
In the step 1; The initialization of said JMS formation comprises: the JMS formation is created by the connection factory through Java EE container provides; From Java order catalogue JNDI, search connection factory through context, create after the connection factory object, use connects factory's establishment and connects; In this connects, create a session, utilize context in container, to pass through JNDI name lookup formation; Through the conversation establishing message producer and information consumption person;
The initialization of said utility command manager comprises: carry out structure utility command pond and start the threading operation that from the JMS formation, obtains command messages;
The initialization of said GPRS service comprises: monitor appointment port, the class that connects time-out time parameter, the login of location registration process remote equipment, data processing, event handling is set.
In step 2; Said W initiation session process is: WEB user interactions layer is after the instruction of receiving the user; Command object COMMAND is encapsulated as JMS message; Said message is made up of header and source body two parts, and header is made up of the metadata of routing iinformation and relevant this message, and source body is then carrying application's data or pay(useful) load.Said source body is divided type according to following pay(useful) load: but the object of plain text serializing, community set, byte stream, original value stream, no pay(useful) load message.
Said step 3 receives JMS message and comprises that creating recipient's object and specifying the reception message mode is the asynchronous system or the method for synchronization; Receive message with the method for synchronization, need the manual message of going to obtain, arrive or in official hour, do not arrive and overtime up to message; During with asynchronous system, client computer need define a message monitor, and when message reached, JMS provider came pass-along message through the onMessage method of calling this object.
In the step 4, after said remote equipment receives orders, return response message through SOCKET then; After receiving the response message of remote equipment; The remote equipment alternation of bed is used at first will resolve to response message RESPONSE according to communication protocol from the byte stream message that SOCKET receives; From the utility command pond, search corresponding command COMMAND by the utility command manager according to type and the device id of response message RESPONSE then; After finding; Deletion should be ordered COMMAND from the utility command pond, and RESPONSE is provided with its corresponding command COMMAND for response, and returns to WEB user interactions layer through the interim formation of JMS.
In the step 4; If remote equipment does not return response message at the appointed time; Then use through the interim formation of JMS and use the prompting of sending a command timeout, and from the utility command pond, remove overtime command object COMMAND to WEB user interactions layer by the remote equipment alternation of bed.
The invention has the advantages that JMS is the part of Java EE technical specification, can with the perfect adaptation of Java EE platform; Through request formula order send mode and interim formation, can for order send and response receive both sides set up efficiently, accurately, reliable message communicating mode; Through the message mechanism of JMS formation, reduce the degree of coupling between using, guaranteed the reliability and the real-time of communication; Through the utility command manager, guaranteed the corresponding relation between order and the response.
Embodiment
For making the object of the invention, technical scheme and advantage clearer, below in conjunction with specific embodiment, to further explain of the present invention.
Be described in further detail below in conjunction with the accompanying drawing specific embodiments of the invention.
Application scenarios of the present invention is as shown in Figure 1, and the user operates the WEB interface, and remote equipment is sent action command, and WEB uses and in a synchronous manner command object COMMAND is encapsulated as the JMS message object, sends in the JMS formation; Data center obtains the JMS message object from the JMS formation; Therefrom extract command object COMMAND; Preserve therein and be used to respond the interim queuing message that returns; Command object COMMAND is put into the order pond, resolve to byte stream according to communication protocol, the network port socket through data center sends to remote equipment; After the response message byte stream that remote equipment returns is received by data center; It is resolved to response message object RESPONSE; In the order pond, search its corresponding command object COMMAND, as find the command object that is complementary, therefrom obtain interim queuing message; Response message RESPONSE is returned to WEB through this interim formation use, further present to the user.
Fig. 2 is an application flow of the present invention.At first the required resource of WEB application system is carried out initialization, comprise JMS formation, utility command manager, GPRS service etc.The establishment of JMS formation is created through the connection factory that Java EE container provides; From Java order catalogue (JNDI), search connection factory through context (Context); Create after the connection factory object, use connection factory to create and connect, in this connection, create a session; Factory is the same with connecting, and formation need utilize context in container, to pass through the JNDI name lookup; Behind the conversation establishing message producer and information consumption person, just can carry out forwards.Following code can be used for accomplishing above-mentioned JMS formation initialization:
Context?context?=?new?InitialContext(properties);
QueueConnectionFactory?queueConnectionFactory?=?(QueueConnectionFactory)?context.lookup(queueConnectionFactoryName);
QueueConnection?queueConnection?=?queueConnectionFactory.createQueueConnection();
queueConnection.start();
QueueSession?queueSession?=?queueConnection.createQueueSession(transacted,?acknowledgementMode);
Queue?queue?=?(Queue)context.lookup(queueName);
The main task of utility command manager is to be the applicating maintenance utility command pond of remote equipment alternation of bed and the operation of in the order pond, searching match responding message the corresponding command, provides to remote equipment and sends command messages and to the service of the application transmitted response message of WEB user interactions layer.The initialization of utility command manager will be carried out structure utility command pond and start the operations such as thread of from the JMS formation, obtaining command messages.
The main task of GPRS service be for application provide with remote equipment between network is connected and foundation and keep both sides' data channel.The operation that need carry out during initialization has: monitor appointment port, the class that connects time-out time parameter, the login of location registration process remote equipment, data processing, event handling etc. is set.
After application initializes is accomplished, just can begin the conversation procedure between WEB application and the remote equipment.WEB user interactions layer is encapsulated as JMS message with command object COMMAND after the instruction of receiving the user, message is a kind of type object among the JMS, is made up of header and source body two parts.Header is made up of the metadata of routing iinformation and relevant this message.Source body is then carrying application's data or pay(useful) load.Type according to pay(useful) load is divided; Can message be divided into several types; They carry respectively: plain text (TextMessage) but the object (ObjectMessage) of serializing, community set (MapMessage), byte stream (BytesMessage), original value stream (StreamMessage) also have or not the message (Message) of pay(useful) load.Send a message to object queue then, object queue is searched acquisition in by context when JMS is initialized.Message is sent has synchronous and asynchronous dual mode.The answer of thread waits message after the method for synchronization transmission message, asynchronous system then need not to wait for.Following code can be realized the definition and the transmission of message:
QueueRequestor?requestor?=?new?QueueRequestor(queueSession,?queue);
ObjectMessage?om?=?queueSession.createObjectMessage();
om.setObject(command);
ObjectMessage?message?=?(ObjectMessage)requestor.request(om);
It is the intermediate layer that WEB is applied to the remote equipment session that the remote equipment alternation of bed is used, and is responsible for receiving from WEB user interactions layer and uses the message of transmitting through JMS, is converted into byte stream, sends to remote equipment through the GPRS service; Simultaneously; Through listens for network port SOCKET; The response word throttling of remote equipment is resolved to response message object RESPONSE; And from the utility command pond of utility command manager, search the corresponding command object COMMAND of response message object RESPONSE, will respond RESPONSE through interim formation and return to the WEB user application layer.
Receive JMS message, must create recipient's object and specify is to receive message with the asynchronous system or the method for synchronization.Receive message with the method for synchronization (Synchronously), need the manual message of going to obtain, in order to obtain a message, the client calls method obtains message, arrives or in official hour, does not arrive and overtime up to message.And during with asynchronous system (Asynchronously), client computer need define a message monitor (MessageListener), and when message reached, JMS provider came pass-along message through the onMessage method of calling this object.
The utility command manager safeguards and to be used for receiving synchronously JMS message by a thread, and following code is that the utility command manager is used for receiving JMS message, and obtains and be used to respond the interim queuing message that returns:
QueueReceiver?queueReceiver?=?queueSession.createReceiver(queue);
Message?message?=?queueReceiver.receive(500);
if(message?==?null)return?null;
ObjectMessage?om?=?(ObjectMessage)message;
TemporaryQueue?replyQueue?=?(TemporaryQueue)om.getJMSReplyTo();
AppCommand?ac?=?(AppCommand)om.getObject();
ac.setResponseTunnel(replyQueue);
Behind the COMMAND that receives orders; Put it into immediately in the utility command pond, according to the regulation of communication protocol, it is resolved to byte stream then; And send to remote equipment through network port SOCKET, following code can be used for sending the byte stream content through SOCKET:
ByteArrayOutputStream?bout?=?new?ByteArrayOutputStream();
bout.write(content);
bout.writeTo(socket.getOutputStream());
After remote equipment receives orders, carry out required movement, return response message through SOCKET then.After receiving the response message of remote equipment; The remote equipment alternation of bed is used at first will resolve to response message RESPONSE according to communication protocol from the byte stream message that SOCKET receives; From the utility command pond, search corresponding command COMMAND by the utility command manager according to type and the device id of response message RESPONSE then; After finding; Deletion should be ordered COMMAND from the utility command pond, and RESPONSE is provided with its corresponding command COMMAND for response, and returns to WEB user interactions layer through the interim formation of JMS.Following code is used for response message RESPONSE is returned through the interim formation of JMS:
TemporaryQueue?replyQueue?=?(TemporaryQueue)?crm.getCommand().getResponseTunnel();
QueueSender?queueSender?=?queueSession.createSender(replyQueue);
ObjectMessage?om?=?queueSession.createObjectMessage();
om.setObject(crm);
queueSender.send(om);
If remote equipment does not return response message at the appointed time; Then use through the interim formation of JMS and use the prompting of sending a command timeout, and from the utility command pond, remove overtime command object COMMAND to WEB user interactions layer by the remote equipment alternation of bed.Following code is used for the overtime response of echo command:
CommandFailMessage failMessage=new CommandFailMessage (command, " command timeout ");
removeAppCommand(command);
commandManager.getMessageTransfer().sendCommandResponse(failMessage);
Above-described specific embodiment; The object of the invention, technical scheme and beneficial effect have been carried out further detailed description, and institute it should be understood that the above is merely specific embodiment of the present invention; Be not limited to the present invention; All within spirit of the present invention and principle, any modification of being made, be equal to replacement, improvement etc., all should be included within protection scope of the present invention.