Biblioteca Java - Rev 31
Subversion Repositories:
(root)/Frameworks and Technologies/TestActiveMQ2/src/main/java/com/linkscreens/activemq/server/ServerMessageHandler.java
package com.linkscreens.activemq.server;
import com.linkscreens.activemq.protocol.MessageProtocol;
import com.linkscreens.activemq.protocol.MessageProtocolFactory;
import com.linkscreens.activemq.protocol.ProtocolType;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.*;
/**
* Receiving messages from client and sending back responses.
*/
public class ServerMessageHandler implements MessageListener {
private MessageProtocol protocol;
private Session session;
private MessageProducer replyProducer;
public ServerMessageHandler(ProtocolType type, Session session, MessageProducer replyProducer) {
this.session = session;
this.replyProducer = replyProducer;
this.protocol = MessageProtocolFactory.GetMessageProtocol(type);
}
public void onMessage(Message message) {
try {
TextMessage response = this.session.createTextMessage();
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String messageText = txtMsg.getText();
response.setText(this.protocol.handleProtocolMessage(messageText));
}
//Set the correlation ID from the received message to be the correlation id of the response message
//this lets the client identify which message this is a response to if it has more than
//one outstanding message to the server
response.setJMSCorrelationID(message.getJMSCorrelationID());
//Send the response to the Destination specified by the JMSReplyTo field of the received message,
//this is presumably a temporary queue created by the client
System.out.println("Sending reply: "+response);
this.replyProducer.send(message.getJMSReplyTo(), response);
} catch (JMSException e) {
//Handle the exception appropriately
e.printStackTrace();
}
}
}
import com.linkscreens.activemq.protocol.MessageProtocol;
import com.linkscreens.activemq.protocol.MessageProtocolFactory;
import com.linkscreens.activemq.protocol.ProtocolType;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.*;
/**
* Receiving messages from client and sending back responses.
*/
public class ServerMessageHandler implements MessageListener {
private MessageProtocol protocol;
private Session session;
private MessageProducer replyProducer;
public ServerMessageHandler(ProtocolType type, Session session, MessageProducer replyProducer) {
this.session = session;
this.replyProducer = replyProducer;
this.protocol = MessageProtocolFactory.GetMessageProtocol(type);
}
public void onMessage(Message message) {
try {
TextMessage response = this.session.createTextMessage();
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String messageText = txtMsg.getText();
response.setText(this.protocol.handleProtocolMessage(messageText));
}
//Set the correlation ID from the received message to be the correlation id of the response message
//this lets the client identify which message this is a response to if it has more than
//one outstanding message to the server
response.setJMSCorrelationID(message.getJMSCorrelationID());
//Send the response to the Destination specified by the JMSReplyTo field of the received message,
//this is presumably a temporary queue created by the client
System.out.println("Sending reply: "+response);
this.replyProducer.send(message.getJMSReplyTo(), response);
} catch (JMSException e) {
//Handle the exception appropriately
e.printStackTrace();
}
}
}