}
Queue responseQueue = config.getResponseQueue();
Connection connection = null;
Session session = null;
JaxbCommandsResponse cmdResponse = null;
String corrId = UUID.randomUUID().toString();
String selector = "JMSCorrelationID = '" + corrId + "'";
try {
// setup
MessageProducer producer;
MessageConsumer consumer;
try {
if( config.getPassword() != null ) {
connection = factory.createConnection(config.getUserName(), config.getPassword());
} else {
connection = factory.createConnection();
}
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(sendQueue);
consumer = session.createConsumer(responseQueue, selector);
connection.start();
} catch( JMSException jmse ) {
throw new RemoteCommunicationException("Unable to setup a JMS connection.", jmse);
}
// Create msg
TextMessage textMsg;
JaxbSerializationProvider serializationProvider;
try {
// serialize request
serializationProvider = config.getJaxbSerializationProvider();
String xmlStr = serializationProvider.serialize(req);
textMsg = session.createTextMessage(xmlStr);
// set properties
// 1. corr id
textMsg.setJMSCorrelationID(corrId);
// 2. serialization info
textMsg.setIntProperty(SERIALIZATION_TYPE_PROPERTY_NAME, config.getSerializationType());
Set<Class<?>> extraJaxbClasses = config.getExtraJaxbClasses();
if( !extraJaxbClasses.isEmpty() ) {
if( deploymentId == null ) {
throw new MissingRequiredInfoException(
"Deserialization of parameter classes requires a deployment id, which has not been configured.");
}
textMsg.setStringProperty(DEPLOYMENT_ID_PROPERTY_NAME, deploymentId);
}
// 3. user/pass for task operations
String userName = config.getUserName();
String password = config.getPassword();
if( isTaskCommand ) {
if( userName == null ) {
throw new RemoteCommunicationException(
"A user name is required when sending task operation requests via JMS");
}
if( password == null ) {
throw new RemoteCommunicationException(
"A password is required when sending task operation requests via JMS");
}
textMsg.setStringProperty("username", userName);
textMsg.setStringProperty("password", password);
}
// 4. process instance id
} catch( JMSException jmse ) {
throw new RemoteCommunicationException("Unable to create and fill a JMS message.", jmse);
} catch( SerializationException se ) {
throw new RemoteCommunicationException("Unable to deserialze JMS message.", se.getCause());
}
// send
try {
producer.send(textMsg);
} catch( JMSException jmse ) {
throw new RemoteCommunicationException("Unable to send a JMS message.", jmse);
}
// receive
Message response;
try {
response = consumer.receive(config.getTimeout() * 1000);
} catch( JMSException jmse ) {
throw new RemoteCommunicationException("Unable to receive or retrieve the JMS response.", jmse);
}
if( response == null ) {
logger.warn("Response is empty");
return null;
}
// extract response
assert response != null: "Response is empty.";
try {
String xmlStr = ((TextMessage) response).getText();
cmdResponse = (JaxbCommandsResponse) serializationProvider.deserialize(xmlStr);
} catch( JMSException jmse ) {
throw new RemoteCommunicationException("Unable to extract " + JaxbCommandsResponse.class.getSimpleName()
+ " instance from JMS response.", jmse);
} catch( SerializationException se ) {
throw new RemoteCommunicationException("Unable to extract " + JaxbCommandsResponse.class.getSimpleName()
+ " instance from JMS response.", se.getCause());
}
assert cmdResponse != null: "Jaxb Cmd Response was null!";
} finally {
if( connection != null ) {
try {
connection.close();
if( session != null ) {
session.close();
}
} catch( JMSException jmse ) {
logger.warn("Unable to close connection or session!", jmse);
}
}
}
String version = cmdResponse.getVersion();
if( version == null ) {
version = "pre-6.0.3";
}
if( !version.equals(VERSION) ) {
logger.info("Response received from server version [{}] while client is version [{}]! This may cause problems.",
version, VERSION);
}
List<JaxbCommandResponse<?>> responses = cmdResponse.getResponses();
if( responses.size() > 0 ) {
JaxbCommandResponse<?> response = responses.get(0);
if( response instanceof JaxbExceptionResponse ) {
JaxbExceptionResponse exceptionResponse = (JaxbExceptionResponse) response;
throw new RemoteApiException(exceptionResponse.getMessage());