SM-2548: Provide CXF Rest Service example for Drools 6 (closes #28) Thanks to Grzegorz Halajko for the pull request!!!
Project: http://git-wip-us.apache.org/repos/asf/servicemix/repo Commit: http://git-wip-us.apache.org/repos/asf/servicemix/commit/8ae57000 Tree: http://git-wip-us.apache.org/repos/asf/servicemix/tree/8ae57000 Diff: http://git-wip-us.apache.org/repos/asf/servicemix/diff/8ae57000 Branch: refs/heads/master Commit: 8ae57000027797d0ca2ab4cef7b78b90a2023db4 Parents: f644027 Author: Grzegorz Halajko <[email protected]> Authored: Tue May 26 23:26:32 2015 +0200 Committer: Krzysztof Sobkowiak <[email protected]> Committed: Wed May 27 00:03:34 2015 +0200 ---------------------------------------------------------------------- .../src/main/filtered-resources/examples.xml | 7 + .../drools/drools-camel-cxf-server/README.txt | 136 +++++++++++++++++++ examples/drools/drools-camel-cxf-server/pom.xml | 104 ++++++++++++++ .../drools/camel/cxf/server/model/Customer.java | 79 +++++++++++ .../camel/cxf/server/model/CustomerType.java | 22 +++ .../cxf/server/osgi/CommandMessageBodyRW.java | 74 ++++++++++ .../server/osgi/DebugAgendaEventListener.java | 132 ++++++++++++++++++ .../osgi/DebugRuleRuntimeEventListener.java | 61 +++++++++ .../drools/camel/cxf/server/osgi/Utils.java | 59 ++++++++ .../OSGI-INF/blueprint/camel-client.xml | 78 +++++++++++ .../OSGI-INF/blueprint/camel-server.xml | 58 ++++++++ .../OSGI-INF/blueprint/kmodule-blueprint.xml | 44 ++++++ .../src/main/resources/rule/customer-score.drl | 48 +++++++ .../src/test/resources/batch-test-request.xml | 14 ++ .../src/test/resources/test-request.xml | 6 + examples/drools/pom.xml | 1 + .../itests/Drools6IntegrationTests.scala | 13 ++ 17 files changed, 936 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/assembly/src/main/filtered-resources/examples.xml ---------------------------------------------------------------------- diff --git a/assembly/src/main/filtered-resources/examples.xml b/assembly/src/main/filtered-resources/examples.xml index 8bfe111..4cbd587 100644 --- a/assembly/src/main/filtered-resources/examples.xml +++ b/assembly/src/main/filtered-resources/examples.xml @@ -177,4 +177,11 @@ <bundle>mvn:org.apache.servicemix.examples/drools-spring/${version}</bundle> </feature> + <feature name="examples-drools-camel-cxf-server" version="${version}" resolver="(obr)"> + <feature version="${drools6.version}">kie-aries-blueprint</feature> + <feature version="${drools6.version}">kie-camel</feature> + <feature version="${camel.version}">camel-xstream</feature> + <bundle>mvn:org.apache.servicemix.examples/drools-camel-cxf-server/${version}</bundle> + </feature> + </features> http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/README.txt ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/README.txt b/examples/drools/drools-camel-cxf-server/README.txt new file mode 100644 index 0000000..aeb6cae --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/README.txt @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +Drools 6 Simple Example +========================== + +Purpose +------- +This example will show you how to use Drools 6 inside Apache ServiceMix and how to +use rule engine in low level. + + + +Prerequisites for Running the Example +------------------------------------- +1. You must have the following installed on your machine: + + - JDK 1.6 or higher + + - Maven 3.0.2 or higher (for building) + + For more information, see the README in the top-level examples + directory. + +2. Start ServiceMix by running the following command: + + <servicemix_home>/bin/servicemix (on UNIX) + <servicemix_home>\bin\servicemix (on Windows) + + +Running the Example +------------------- +To install and run the example where you build the example bundle +yourself, complete the following steps: + +1. Before being able to run this example, you have to install some additional + features into the container first to add support for the Drools 6. + + feature:install kie-aries-blueprint + feature:install kie-camel + feature:install camel-xstream + +2. Build the example by opening a command prompt, changing directory to + examples/drools/drools-simple (this example) and entering the following Maven + command: + + mvn clean install + + If all of the required OSGi bundles are available in your local Maven + repository, the example will build very quickly. Otherwise it may + take some time for Maven to download everything it needs. + + The mvn install command builds the example deployment bundle and + copies it to your local Maven repository and to the target directory + of this example. + +3. Install the example by entering the following command in + the ServiceMix console: + + bundle:install -s mvn:org.apache.servicemix.examples/drools-camel-cxf-server/${project.version} + +4. Once the bundle has been started, you will see on console logs from rule engine. + +drools-camel-cxf-server - 6.0.0.SNAPSHOT | objectInserted ==>[ObjectInsertedEventImpl: +getFactHandle()=[fact 0:1:109283499:109283499:1:DEFAULT:NON_TRAIT: +Customer [salary=3955, type=null]], getObject()=Customer [salary=3955, type=null], +getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@45a5e94a, getPropagationContext()=PhreakPropagationContext +[entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:109283499:109283499:1:DEFAULT:NON_TRAIT:Customer +[salary=3955, type=null]], leftTuple=null, originOffset=-1, propagationNumber=2, rule=null, type=0]] + +DroolsRestResult Exchange[ExchangePattern: InOut, BodyType: String, +Body: <?xml version='1.0' encoding='UTF-8'?> + <execution-results> + <result identifier="customer"> + <org.apache.servicemix.examples.drools.simple.model.Customer> + <salary>3955</salary> + <type>NORMAL</type> + </org.apache.servicemix.examples.drools.simple.model.Customer> + </result> + <fact-handle identifier="customer" external-form="0:1:878677865:878677865:2:DEFAULT:NON_TRAIT"/> + </execution-results>] + + Timer on camel route call rest client procedure. Via rest service facts it's inserted to work memory + and in logs you can saw drools engine activity + +5. You can use a command-line utility, such as curl or Wget, to make the invocations. + For example, try using curl as follows: + + # + # Single fact execution: + # + curl -X POST -T src/test/resources/test-request.xml -H "Content-Type: text/plain" http://localhost:8181/cxf/rest/execute + + # + # Batch execution (multi facts insert): + # + curl -X POST -T src/test/resources/batch-test-request.xml -H "Content-Type: text/plain" http://localhost:8181/cxf/rest/execute + +Stopping and Uninstalling the Example +------------------------------------- +First, find the bundle id for the deployed example bundle by doing + + osgi:list + +and looking for a line that looks like this one + +229 | Active | 80 | | Apache ServiceMix :: Examples :: Drools :: Camel CXF Server + +In the above case, the bundle id would be 229 + + +To stop the example, enter the following command in the ServiceMix +console: + + bundle:stop <bundle_id> + + +To uninstall the example, enter one of the following commands in +the ServiceMix console: + + bundle:uninstall <bundle_id> + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/pom.xml b/examples/drools/drools-camel-cxf-server/pom.xml new file mode 100644 index 0000000..adf2d19 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/pom.xml @@ -0,0 +1,104 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version + 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.servicemix.examples</groupId> + <artifactId>drools</artifactId> + <version>6.0.0-SNAPSHOT</version> + </parent> + + <artifactId>drools-camel-cxf-server</artifactId> + <name>Apache ServiceMix :: Examples :: Drools :: Camel CXF Server</name> + <packaging>bundle</packaging> + + <properties> + <osgi.private.package> + org.apache.servicemix.examples.drools.camel.cxf.server.osgi, + org.apache.servicemix.examples.drools.camel.cxf.server.model</osgi.private.package> + <osgi.import.package> + org.osgi.service.blueprint, + org.slf4j;version="[1.6,2)", + org.kie.jax.rs;version="[6.0,7.0)", + org.kie.camel.component;version="[6.0,7.0)", + org.kie.api.event.rule;version="[6.0,7.0)", + org.kie.api.command;version="[6.0,7.0)", + org.kie.aries.blueprint.factorybeans;version="[6.0,7.0)", + org.apache.camel;version="[2.15,2.16)", + + org.kie.internal.command;version="[6.0,7)", + + org.kie.internal.runtime.helper;version="[6.0,7)", + org.drools.core.runtime.impl;version="[6.0,7)", + org.drools.core.runtime.rule.impl;version="[6.0,7)", + org.drools.core.command.runtime.process;version="[6.0,7)", + org.drools.core.common;version="[6.0,7)", + org.drools.core.command.runtime;version="[6.0,7)", + org.drools.core.command.runtime.rule;version="[6.0,7)", + org.drools.core.impl;version="[6.0,7)", + org.drools.core.runtime.help.impl;version="[6.0,7)", + com.thoughtworks.xstream;version="1.4", + javax.ws.rs.ext, + javax.ws.rs.core, + javax.ws.rs + </osgi.import.package> + </properties> + + <dependencies> + + <dependency> + <groupId>org.kie</groupId> + <artifactId>kie-api</artifactId> + </dependency> + + <dependency> + <groupId>org.kie</groupId> + <artifactId>kie-camel</artifactId> + </dependency> + + <dependency> + <groupId>org.drools</groupId> + <artifactId>drools-compiler</artifactId> + </dependency> + + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-cxf</artifactId> + </dependency> + + </dependencies> + + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/Customer.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/Customer.java b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/Customer.java new file mode 100644 index 0000000..98e2b5d --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/Customer.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.camel.cxf.server.model; + +public class Customer { + + /** + * Salary + */ + private int salary; + + /** + * IS VIP. + */ + private CustomerType type; + + /** + * Create customer with salary. + * + * @param salary + */ + public Customer(int salary) { + this.salary = salary; + } + + /** + * @return the salary + */ + public int getSalary() { + return salary; + } + + /** + * @return the type + */ + public CustomerType getType() { + return type; + } + + /** + * @param salary the salary to set + */ + public void setSalary(int salary) { + this.salary = salary; + } + + /** + * @param type the type to set + */ + public void setType(CustomerType type) { + this.type = type; + } + + /** + * @see Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Customer [salary=").append(salary).append(", type=") + .append(type).append("]"); + return builder.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/CustomerType.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/CustomerType.java b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/CustomerType.java new file mode 100644 index 0000000..b969c67 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/model/CustomerType.java @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.camel.cxf.server.model; + + +public enum CustomerType { + POOR, NORMAL, VIP; +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/CommandMessageBodyRW.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/CommandMessageBodyRW.java b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/CommandMessageBodyRW.java new file mode 100644 index 0000000..6889982 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/CommandMessageBodyRW.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.camel.cxf.server.osgi; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +import org.drools.core.runtime.help.impl.BatchExecutionHelperProviderImpl; +import org.kie.api.command.Command; +import org.kie.internal.runtime.helper.BatchExecutionHelperProvider; + + +@Consumes("text/plain") +@Provider +public class CommandMessageBodyRW implements MessageBodyWriter<Command<?>> { + + /** + * provider + */ + private BatchExecutionHelperProvider provider = new BatchExecutionHelperProviderImpl(); + + /** + * getSize + */ + @Override + public long getSize(Command<?> arg0, Class<?> arg1, Type arg2, + Annotation[] arg3, MediaType arg4) { + return -1; + } + + /** + * isWriteable + */ + @Override + public boolean isWriteable(Class<?> type, Type genericType, + Annotation[] arg2, MediaType arg3) { + return Command.class.isAssignableFrom(type); + + } + + /** + * writeTo + */ + @Override + public void writeTo(Command<?> obj, Class<?> arg1, Type arg2, + Annotation[] arg3, MediaType arg4, + MultivaluedMap<String, Object> arg5, OutputStream out) + throws IOException, WebApplicationException { + provider.newXStreamMarshaller().toXML(obj, out); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugAgendaEventListener.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugAgendaEventListener.java b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugAgendaEventListener.java new file mode 100644 index 0000000..bad7479 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugAgendaEventListener.java @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.camel.cxf.server.osgi; + +import org.kie.api.event.rule.AfterMatchFiredEvent; +import org.kie.api.event.rule.AgendaEventListener; +import org.kie.api.event.rule.AgendaGroupPoppedEvent; +import org.kie.api.event.rule.AgendaGroupPushedEvent; +import org.kie.api.event.rule.BeforeMatchFiredEvent; +import org.kie.api.event.rule.MatchCancelledEvent; +import org.kie.api.event.rule.MatchCreatedEvent; +import org.kie.api.event.rule.RuleFlowGroupActivatedEvent; +import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Drools 6 Rule Engine events logger. + */ +public class DebugAgendaEventListener implements AgendaEventListener { + /** + * Logger. + */ + private static final Logger log = LoggerFactory + .getLogger(DebugAgendaEventListener.class); + + /** + * @see AgendaEventListener#matchCreated(org.kie.api.event.rule.MatchCreatedEvent) + */ + @Override + public void matchCreated(MatchCreatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#matchCancelled(org.kie.api.event.rule.MatchCancelledEvent) + */ + @Override + public void matchCancelled(MatchCancelledEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent) + */ + @Override + public void beforeMatchFired(BeforeMatchFiredEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent) + */ + @Override + public void afterMatchFired(AfterMatchFiredEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent) + */ + @Override + public void agendaGroupPopped(AgendaGroupPoppedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent) + */ + @Override + public void agendaGroupPushed(AgendaGroupPushedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) + */ + @Override + public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) + */ + @Override + public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) + */ + @Override + public void beforeRuleFlowGroupDeactivated( + RuleFlowGroupDeactivatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) + */ + @Override + public void afterRuleFlowGroupDeactivated( + RuleFlowGroupDeactivatedEvent event) { + log.info("{}", event); + + } +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugRuleRuntimeEventListener.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugRuleRuntimeEventListener.java b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugRuleRuntimeEventListener.java new file mode 100644 index 0000000..71a8229 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/DebugRuleRuntimeEventListener.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.camel.cxf.server.osgi; + +import org.kie.api.event.rule.ObjectDeletedEvent; +import org.kie.api.event.rule.ObjectInsertedEvent; +import org.kie.api.event.rule.ObjectUpdatedEvent; +import org.kie.api.event.rule.RuleRuntimeEventListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Drools 6 Working memory events. + */ +public class DebugRuleRuntimeEventListener implements RuleRuntimeEventListener { + /** + * Logger. + */ + private static final Logger log = LoggerFactory + .getLogger(DebugRuleRuntimeEventListener.class); + + /** + * @see RuleRuntimeEventListener#objectInserted(ObjectInsertedEvent) + */ + @Override + public void objectInserted(ObjectInsertedEvent event) { + log.info("objectInserted {}", event); + } + + /** + * @see RuleRuntimeEventListener#objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent) + */ + @Override + public void objectUpdated(ObjectUpdatedEvent event) { + log.info("objectUpdated {}", event); + + } + + /** + * @see RuleRuntimeEventListener#objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent) + */ + @Override + public void objectDeleted(ObjectDeletedEvent event) { + log.info("objectDeleted {}", event); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/Utils.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/Utils.java b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/Utils.java new file mode 100644 index 0000000..104a69e --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/java/org/apache/servicemix/examples/drools/camel/cxf/server/osgi/Utils.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.camel.cxf.server.osgi; + +import java.util.Arrays; +import java.util.Random; + +import org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer; +import org.kie.api.command.Command; +import org.kie.api.runtime.ExecutionResults; +import org.kie.internal.command.CommandFactory; + + +public class Utils { + /** + * Random + */ + private static Random rand = new Random(12345); + + /** + * Generate random customer + * + * @return + */ + public Customer customer() { + return new Customer(rand.nextInt(9999)); + } + + /** + * Create commands for Drools engine. + * + * @param body {@link org.apache.camel.Exchange} + */ + public Command<ExecutionResults> insertAndFireAll(final Customer body) { + + Command<?> insert = CommandFactory.newInsert(body, "customer"); + + @SuppressWarnings("unchecked") + Command<ExecutionResults> batch = CommandFactory + .newBatchExecution(Arrays.asList(insert)); + + return batch; + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-client.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-client.xml b/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-client.xml new file mode 100644 index 0000000..69f6d7b --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-client.xml @@ -0,0 +1,78 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" + xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf" + xmlns:cxf-core="http://cxf.apache.org/blueprint/core" + xsi:schemaLocation=" +http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd +http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd +http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/cxf/camel-cxf-blueprint.xsd +http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd +http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> + + <!-- Rest Converter. Convert object extends by org.kie.api.command.Command + to string --> + <bean id="resultMessageProvider" + class="org.apache.servicemix.examples.drools.camel.cxf.server.osgi.CommandMessageBodyRW" /> + + <!-- Camel REST Client --> + <cxf:rsClient id="droolsRestClient" address="http://localhost:8181/cxf/rest" + serviceClass="org.kie.jax.rs.CommandExecutorImpl"> + <cxf:providers> + <ref component-id="resultMessageProvider" /> + </cxf:providers> + </cxf:rsClient> + + <!-- Utils. --> + <bean id="customerUtil" + class="org.apache.servicemix.examples.drools.camel.cxf.server.osgi.Utils" /> + + <!-- Bean insert to Camel message configuration parameters + for cxf camel component --> + <bean id="preCxfrs" class="org.kie.camel.component.PreCxfrs" /> + + <!-- Bean trim response and convert request from InputStream to String --> + <bean id="postCxfrs" class="org.kie.camel.component.PostCxfrs" /> + + <!-- CAMEL --> + <camelContext id="camel-client" + xmlns="http://camel.apache.org/schema/blueprint"> + + <route id="route-to-rest"> + <!-- Self-Timer --> + <from + uri="timer:restClientMock?fixedRate=true&period=30s&delay=5000" /> + <process ref="preCxfrs" /> + <!-- Create Customer with random salary --> + <bean method="customer" ref="customerUtil" /> + <!-- Create KIE Commands --> + <bean method="insertAndFireAll" ref="customerUtil" /> + <!-- Run CXF Rest client --> + <to uri="cxfrs:bean:droolsRestClient" /> + <process ref="postCxfrs" /> + <!-- The result it's printed in the logs. --> + <to uri="log:DroolsRestResult" /> + </route> + + + </camelContext> + +</blueprint> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-server.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-server.xml b/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-server.xml new file mode 100644 index 0000000..263096e --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/camel-server.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" + xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf" + xmlns:cxf-core="http://cxf.apache.org/blueprint/core" + xsi:schemaLocation=" +http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd +http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd +http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd +http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd"> + + <!-- Bean trim request string --> + <bean id="messageBodyReader" class="org.kie.jax.rs.CommandMessageBodyReader" /> + <!-- Bean add support drools, catch rest request and run KIE Commands--> + <bean id="kiePolicy" class="org.kie.camel.component.KiePolicy" /> + + <!-- Apache Camel Rest Server --> + <cxf:rsServer id="restKieServer" address="/rest" + serviceClass="org.kie.jax.rs.CommandExecutorImpl"> + <cxf:providers> + <ref component-id="messageBodyReader" /> + </cxf:providers> + </cxf:rsServer> + + <!-- CAMEL --> + <camelContext id="camel" + xmlns="http://camel.apache.org/schema/blueprint"> + <!-- Route catch REST request and forward request to Kie Policy Bean--> + <route> + <from uri="cxfrs:bean:restKieServer" /> + <policy ref="kiePolicy"> + <unmarshal ref="xstream" /> + <to uri="kie:simpleRuleKSession" /> + <marshal ref="xstream" /> + </policy> + </route> + + </camelContext> + +</blueprint> http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml b/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml new file mode 100644 index 0000000..b299caa --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<blueprint default-timeout="1000" + xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:kie="http://drools.org/schema/kie-aries-blueprint/1.0.0" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0 + http://drools.org/schema/kie-aries-blueprint/1.0.0 http://drools.org/schema/kie-aries-blueprint/1.0.0"> + + <bean id="DebugAgendaEventListener" + class="org.apache.servicemix.examples.drools.camel.cxf.server.osgi.DebugAgendaEventListener" /> + <bean id="DebugRuleRuntimeEventListener" + class="org.apache.servicemix.examples.drools.camel.cxf.server.osgi.DebugRuleRuntimeEventListener" /> + + <kie:eventListeners id="debugListeners"> + <kie:agendaEventListener ref="DebugAgendaEventListener" /> + <kie:ruleRuntimeEventListener ref="DebugRuleRuntimeEventListener" /> + </kie:eventListeners> + + <kie:kmodule id="id-kmodule"> + <kie:kbase name="SimpleRuleKBase" default="true" + packages="rule"> + <kie:ksession name="simpleRuleKSession" type="stateless" + listeners-ref="debugListeners" default="true"> + </kie:ksession> + </kie:kbase> + </kie:kmodule> + +</blueprint> http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/main/resources/rule/customer-score.drl ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/main/resources/rule/customer-score.drl b/examples/drools/drools-camel-cxf-server/src/main/resources/rule/customer-score.drl new file mode 100644 index 0000000..bc1e3d7 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/main/resources/rule/customer-score.drl @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package rule; +import org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer; +import org.apache.servicemix.examples.drools.camel.cxf.server.model.CustomerType; + +rule "Customer poor" +when + $c : Customer( salary <= 1000, type == null) +then + modify( $c ) { + setType(CustomerType.POOR) + }; +end + + +rule "Customer NORMAL" +when + $c : Customer( salary > 1000, salary <= 9000, type == null ) +then + modify( $c ) { + setType(CustomerType.NORMAL) + }; +end + + +rule "Customer VIP" +when + $c : Customer( salary > 9000, type == null) +then + modify( $c ) { + setType(CustomerType.VIP) + }; +end \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/test/resources/batch-test-request.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/test/resources/batch-test-request.xml b/examples/drools/drools-camel-cxf-server/src/test/resources/batch-test-request.xml new file mode 100644 index 0000000..2824888 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/test/resources/batch-test-request.xml @@ -0,0 +1,14 @@ +<batch-execution lookup="rule-num"> + <insert out-identifier="cusomer1" return-object="true" + entry-point="DEFAULT"> + <org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer> + <salary>9000</salary> + </org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer> + </insert> + <insert out-identifier="cusomer2" return-object="true" + entry-point="DEFAULT"> + <org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer> + <salary>1000</salary> + </org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer> + </insert> +</batch-execution> http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/drools-camel-cxf-server/src/test/resources/test-request.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-cxf-server/src/test/resources/test-request.xml b/examples/drools/drools-camel-cxf-server/src/test/resources/test-request.xml new file mode 100644 index 0000000..0359247 --- /dev/null +++ b/examples/drools/drools-camel-cxf-server/src/test/resources/test-request.xml @@ -0,0 +1,6 @@ +<insert out-identifier="cusomer1" return-object="true" + entry-point="DEFAULT"> + <org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer> + <salary>9000</salary> + </org.apache.servicemix.examples.drools.camel.cxf.server.model.Customer> +</insert> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/examples/drools/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/pom.xml b/examples/drools/pom.xml index 97291cd..564c37f 100644 --- a/examples/drools/pom.xml +++ b/examples/drools/pom.xml @@ -76,6 +76,7 @@ <modules> <module>drools-simple</module> <module>drools-camel-blueprint</module> + <module>drools-camel-cxf-server</module> <module>drools-spring</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/servicemix/blob/8ae57000/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala ---------------------------------------------------------------------- diff --git a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala index 371e138..a197c37 100644 --- a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala +++ b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala @@ -117,3 +117,16 @@ class Drools6SpringExamplesTest extends Drools6IntegrationTests { } } } + +/** + * Tests for the Camel Rest Server examples + */ +class Drools6CamelServiceExamplesTest extends Drools6IntegrationTests { + + @Test + def testDroolsCamelServerExample = testWithFeature("examples-drools-camel-cxf-server") { + expect { + logging.containsMessage(line => line.contains("<execution-results><result identifier=\"customer\">")) + } + } +}
