use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.
the class KieServerBackwardCompatDroolsIntegrationTest method testCallContainerWithStringPayload.
@Test
public void testCallContainerWithStringPayload() throws Exception {
Marshaller marshaller = MarshallerFactory.getMarshaller(new HashSet<Class<?>>(extraClasses.values()), configuration.getMarshallingFormat(), kjarClassLoader);
Object message = createInstance(MESSAGE_CLASS_NAME);
KieServerReflections.setValue(message, MESSAGE_TEXT_FIELD, MESSAGE_REQUEST);
List<Command<?>> commands = new ArrayList<Command<?>>();
BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands, KIE_SESSION);
commands.add(commandsFactory.newInsert(message, MESSAGE_OUT_IDENTIFIER));
commands.add(commandsFactory.newFireAllRules());
String marshalledCommands = marshaller.marshall(batchExecution);
ServiceResponse<String> reply = client.executeCommands(CONTAINER_ID, marshalledCommands);
Assert.assertEquals(ServiceResponse.ResponseType.SUCCESS, reply.getType());
ExecutionResults results = marshaller.unmarshall(reply.getResult(), ExecutionResultImpl.class);
Object value = results.getValue(MESSAGE_OUT_IDENTIFIER);
Assert.assertEquals(MESSAGE_RESPONSE, KieServerReflections.valueOf(value, MESSAGE_TEXT_FIELD));
}
use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.
the class KieServerBackwardCompatDroolsIntegrationTest method testCallContainer.
@Test
public void testCallContainer() throws Exception {
Marshaller marshaller = MarshallerFactory.getMarshaller(new HashSet<Class<?>>(extraClasses.values()), configuration.getMarshallingFormat(), kjarClassLoader);
Object message = createInstance(MESSAGE_CLASS_NAME);
KieServerReflections.setValue(message, MESSAGE_TEXT_FIELD, MESSAGE_REQUEST);
List<Command<?>> commands = new ArrayList<Command<?>>();
BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands, KIE_SESSION);
commands.add(commandsFactory.newInsert(message, MESSAGE_OUT_IDENTIFIER));
commands.add(commandsFactory.newFireAllRules());
ServiceResponse<String> reply = client.executeCommands(CONTAINER_ID, batchExecution);
Assert.assertEquals(ServiceResponse.ResponseType.SUCCESS, reply.getType());
ExecutionResults results = marshaller.unmarshall(reply.getResult(), ExecutionResultImpl.class);
Object value = results.getValue(MESSAGE_OUT_IDENTIFIER);
Assert.assertEquals(MESSAGE_RESPONSE, KieServerReflections.valueOf(value, MESSAGE_TEXT_FIELD));
}
use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.
the class KieServerBackwardCompatDroolsIntegrationTest method testCallContainerRuleClient.
@Test
public void testCallContainerRuleClient() throws Exception {
Marshaller marshaller = MarshallerFactory.getMarshaller(new HashSet<Class<?>>(extraClasses.values()), configuration.getMarshallingFormat(), kjarClassLoader);
Object message = createInstance(MESSAGE_CLASS_NAME);
KieServerReflections.setValue(message, MESSAGE_TEXT_FIELD, MESSAGE_REQUEST);
List<Command<?>> commands = new ArrayList<Command<?>>();
BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands, KIE_SESSION);
commands.add(commandsFactory.newInsert(message, MESSAGE_OUT_IDENTIFIER));
commands.add(commandsFactory.newFireAllRules());
ServiceResponse<String> reply = ruleClient.executeCommands(CONTAINER_ID, batchExecution);
Assert.assertEquals(ServiceResponse.ResponseType.SUCCESS, reply.getType());
ExecutionResults results = marshaller.unmarshall(reply.getResult(), ExecutionResultImpl.class);
Object value = results.getValue(MESSAGE_OUT_IDENTIFIER);
Assert.assertEquals(MESSAGE_RESPONSE, KieServerReflections.valueOf(value, MESSAGE_TEXT_FIELD));
}
use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.
the class KieServerDroolsIntegrationTest method testCommandScript.
@Test
@Category(Smoke.class)
public void testCommandScript() throws Exception {
Marshaller marshaller = MarshallerFactory.getMarshaller(new HashSet<Class<?>>(extraClasses.values()), configuration.getMarshallingFormat(), kjarClassLoader);
Object message = createInstance(MESSAGE_CLASS_NAME);
KieServerReflections.setValue(message, MESSAGE_TEXT_FIELD, MESSAGE_REQUEST);
Command<?> insert = commandsFactory.newInsert(message, MESSAGE_OUT_IDENTIFIER);
Command<?> fire = commandsFactory.newFireAllRules();
BatchExecutionCommand batch = commandsFactory.newBatchExecution(Arrays.<Command<?>>asList(insert, fire), KIE_SESSION);
String payload = marshaller.marshall(batch);
String containerId = "command-script-container";
KieServerCommand create = new CreateContainerCommand(new KieContainerResource(containerId, releaseIdScript, null));
KieServerCommand call = new CallContainerCommand(containerId, payload);
KieServerCommand dispose = new DisposeContainerCommand(containerId);
List<KieServerCommand> cmds = Arrays.asList(create, call, dispose);
CommandScript script = new CommandScript(cmds);
ServiceResponsesList reply = client.executeScript(script);
for (ServiceResponse<? extends Object> r : reply.getResponses()) {
Assert.assertEquals(ServiceResponse.ResponseType.SUCCESS, r.getType());
}
}
use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.
the class KieServerMDB method onMessage.
public void onMessage(Message message) {
JMSConnection connect = null;
try {
String username = null;
String password = null;
try {
username = message.getStringProperty(USER_PROPERTY_NAME);
password = message.getStringProperty(PASSWRD_PROPERTY_NAME);
} catch (JMSException jmse) {
logger.warn("Unable to retrieve user name and/or password, from message");
}
if (username != null && password != null) {
JMSSecurityAdapter.login(username, password);
} else {
logger.warn("Unable to login to JMSSecurityAdapter, user name and/or password missing");
}
KieContainerCommandService executor = null;
// 0. Get msg correlation id (for response)
String msgCorrId = null;
try {
msgCorrId = message.getJMSCorrelationID();
} catch (JMSException jmse) {
String errMsg = "Unable to retrieve JMS correlation id from message! " + ID_NECESSARY;
throw new JMSRuntimeException(errMsg, jmse);
}
// for backward compatibility default to KieServer
String targetCapability = getStringProperty(message, TARGET_CAPABILITY_PROPERTY_NAME, "KieServer");
String containerId = getStringProperty(message, CONTAINER_ID_PROPERTY_NAME, null);
String conversationId = getStringProperty(message, CONVERSATION_ID_PROPERTY_NAME, null);
int interactionPattern = getIntProperty(message, INTERACTION_PATTERN_PROPERTY_NAME, REQUEST_REPLY_PATTERN);
// 1. get marshalling info
MarshallingFormat format = null;
String classType = null;
try {
classType = message.getStringProperty(CLASS_TYPE_PROPERTY_NAME);
if (!message.propertyExists(SERIALIZATION_FORMAT_PROPERTY_NAME)) {
format = MarshallingFormat.JAXB;
} else {
int intFormat = message.getIntProperty(SERIALIZATION_FORMAT_PROPERTY_NAME);
logger.debug("Serialization format (int) is {}", intFormat);
format = MarshallingFormat.fromId(intFormat);
logger.debug("Serialization format is {}", format);
if (format == null) {
String errMsg = "Unsupported marshalling format '" + intFormat + "' from message " + msgCorrId + ".";
throw new JMSRuntimeException(errMsg);
}
}
} catch (JMSException jmse) {
String errMsg = "Unable to retrieve property '" + SERIALIZATION_FORMAT_PROPERTY_NAME + "' from message " + msgCorrId + ".";
throw new JMSRuntimeException(errMsg, jmse);
}
// 2. get marshaller
Marshaller marshaller = getMarshaller(containerId, format);
logger.debug("Selected marshaller is {}", marshaller);
// 3. deserialize request
CommandScript script = unmarshallRequest(message, msgCorrId, marshaller, format);
logger.debug("Target capability is {}", targetCapability);
for (KieServerExtension extension : kieServer.getServerExtensions()) {
KieContainerCommandService tmp = extension.getAppComponents(KieContainerCommandService.class);
if (tmp != null && extension.getImplementedCapability().equalsIgnoreCase(targetCapability)) {
executor = tmp;
logger.debug("Extension {} returned command executor {} with capability {}", extension, executor, extension.getImplementedCapability());
break;
}
}
if (executor == null) {
throw new IllegalStateException("No executor found for script execution");
}
// 4. process request
ServiceResponsesList response = executor.executeScript(script, format, classType);
if (interactionPattern < UPPER_LIMIT_REPLY_INTERACTION_PATTERNS) {
connect = startConnectionAndSession();
logger.debug("Response message is about to be sent according to selected interaction pattern {}", interactionPattern);
// 5. serialize response
Message msg = marshallResponse(connect.getSession(), msgCorrId, format, marshaller, response);
// set conversation id for routing
if (containerId != null && (conversationId == null || conversationId.trim().isEmpty())) {
try {
KieContainerInstance containerInstance = kieServer.getServerRegistry().getContainer(containerId);
if (containerInstance != null) {
ReleaseId releaseId = containerInstance.getResource().getResolvedReleaseId();
if (releaseId == null) {
releaseId = containerInstance.getResource().getReleaseId();
}
conversationId = ConversationId.from(KieServerEnvironment.getServerId(), containerId, releaseId).toString();
}
} catch (Exception e) {
logger.warn("Unable to build conversation id due to {}", e.getMessage(), e);
}
}
try {
if (conversationId != null) {
msg.setStringProperty(CONVERSATION_ID_PROPERTY_NAME, conversationId);
}
} catch (JMSException e) {
logger.debug("Unable to set conversation id on response message due to {}", e.getMessage());
}
// 6. send response
sendResponse(connect.getSession(), msgCorrId, format, msg);
} else {
logger.debug("Response message is skipped according to selected interaction pattern {}", FIRE_AND_FORGET_PATTERN);
}
} finally {
if (connect != null) {
// Only attempt to close the connection/session if they were actually created
try {
closeConnectionAndSession(connect);
} catch (JMSRuntimeException runtimeException) {
logger.error("Error while attempting to close connection/session", runtimeException);
} finally {
JMSSecurityAdapter.logout();
}
} else {
JMSSecurityAdapter.logout();
}
}
}
Aggregations