Configuring drools and maven and writing hello world application using them - java

I want to learn drools and maven can any one help me with the links for configuring drools and maven and writing a basic hello world example using them.
Thanks in advance

You should first read the manual, then try google it. There have also been questions like this asked before, for example: How to deploy Drools Flow and rules by my web application
But anyways. This is how to integrate it if you use Maven and Spring:
you first need to include Drools dependencies:
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-spring</artifactId>
<version>${drools.version}</version>
</dependency>
Define the application context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:drools="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd">
<drools:kbase id="kbase1">
<drools:resources>
<drools:resource source="classpath:Sample.drl" />
</drools:resources>
</drools:kbase>
<drools:ksession id="ksession1" type="stateful" kbase="kbase1" />
</beans>
Then you can inject ksession1 as a bean.

Try google: First result for Drools Maven Tutorial: http://www.installationwiki.org/Set_Up_for_JBoss_Drools.
Note that Maven is a "build" tool (actually it's more) whereas Drools is a platform/library. So using Drools does normally not depend on Maven. You might thus want to get separate tutorials.

Related

How to import Spring application context from another maven module?

I have two modules in my project which are children of the main app.pom
<modules>
<module>commons</module>
<module>webapp</module>
</modules>
The webapp module refers to commons this way:
<dependency>
<groupId>com.app.whatever</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
</dependency>
The commons module is just packaged in .jar file and that's it. All their dependencies webapp and commons inherit from app.pom.
Both commons and webapp use Spring 3 with .xml files that describe application contexts.
In webapp module I want to use a bean from commons module. Here's applicationContext for commons:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.app.whatever.anything"/>
</beans>
For webapp applicationContext.xml I have:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="classpath*:/applicationContext-commons.xml"/>
<context:component-scan base-package="com.app.whatever.something.controller.spring"/>
But unfortunately this doesn't work. Constantly get an exception about no candidates for autowiring, though I can use any classes from the second module in the first one after setting the above maven dependency.
When I write this import without asterisk, it says that the file doesn't exist.
I tried without leading slash - the same exception.
This import works perfectly for any file from the same module and even for external libraries I have. But it can't resolve the file from this new commons module I've just created.
If you have any idea, that'd be really helpful, thanks
If it's not under a subdirectory, e.g. spring/applicationContext-commons.xml then the following (which you've already tried) should work:
<import resource="classpath*:applicationContext-commons.xml"/>
With an asterisk, Spring will use all matching files on the classpath, without the asterisk it will use the first one it finds.
Also, with the asterisk Spring will ignore the file if it wasn't found. Without the asterisk Spring will give an error if it can't find the file. Therefore your file is not on the classpath.
I'd check the location of applicationContext-commons.xml under the target folder to make sure <import resource= has the correct path and re-run mvn install from the root pom.

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace

I am facing a lot of issues one after the other. Let me note it down properly -
I am implementing Springframework cache and this was my original SpringCacheConfig.xml -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.hazelcast.com/schema/spring http://www.hazelcast.com/schema/spring/hazelcast-spring.xsd">
This was working fine in my laptop, but in the test VM, we were not able to download the XSD schema files from internet.
So I changed the schemalocations to classpath -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:spring-beans.xsd
http://www.springframework.org/schema/cache classpath:spring-cache.xsd
http://www.springframework.org/schema/context classpath:spring-context.xsd
http://www.hazelcast.com/schema/spring classpath:hazelcast-spring.xsd">
So now the XSD files were picked up. But the downloaded spring-context.xsd file has the following content -
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="https://www.springframework.org/schema/beans/spring-beans-4.3.xsd"/>
<xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="https://www.springframework.org/schema/tool/spring-tool-4.3.xsd"/>
So I have again moved them to classpath.
After these changes if I now execute our code, I am getting the following error -
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [SpringCacheConfig.xml]
I tried to solve this problem by many previous posts, but not able to.
The pom is already including spring-context related jars -
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
These jars are not packaged within the main jar, but are available at
modules/system/layers/thirdparty/org/springframework/main/spring-context-4.3.1.RELEASE.jar.
But why the jar couldn't be found?
I also tried the shade plugin, but still dependency jars are not included -
How to create spring-based executable jar with maven?
What else should I think about?
I don't have the META-INF/spring.handlers etc Spring related files within the jar. Can it be the issue?
I solved the problem by creating a META-INF directory and putting the spring.handlers and spring.schemas files into it. I extracted all the spring jars, some of them have these spring.handlers and spring.schemas files. I concatenated the contents of those files and put them in META-INF.
But surprisingly, the project was working in eclipse, but not in VM. In eclipse project, I don't need to copy the spring.handlers and spring.schemas files in the jar's META-INF directory - it works without them. But in VM I need to copy the files! May be in eclipse these files are referred from .m2, since those jars are in the classpath? Any idea?
Thanks

Upgrade from spring 4.3.2 to 5.0.2 is causing missing #AliasFor error

When I try to upgrade a spring web-mvc application from version 4.3.2 to 5.0.2, I get an error in servlet xml. The error happens in the following line,
<context:component-scan base-package="com.mypackage" />
Error is as follows,
Error occured processing XML '#AliasFor declaration on attribute [value] in annotation [org.springframework.stereotype.Controller] is missing required 'attribute' value.'. See Error Log for more details
This is how my servlet xml looks like.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd">
<security:global-method-security secured-annotations="enabled"/>
<context:component-scan base-package="com.mypackage" />
<mvc:view-controller path="/" view-name="index"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
If I use 5.0.0 this error is not happening. As it is complaining about a missing annotation in org.springframework.stereotype.Controller, I tried to find out what is newly added or missing in the Controller. I could see that a new line is added in version 5.0.2 and it has a missing 'attribute'.
#AliasFor(annotation = Component.class)
I am sure I am missing something in my bean class definition which is required from version 5.0.2.
I'd check for three things.
First, you need to make sure that your dependency management is right. You can run mvn dependency:tree and check that all Spring dependencies are using the same version. If you want to avoid issues like this in the future, you can use the Spring Framework BOM and let it manage versions for Spring Framework artifacts:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
If your dependency management is right, this might be a corrupted JAR. You can clear things and re-download dependencies with mvn dependency:purge-local-repository.
Finally, using versioned schemas in your XML is not advised anymore - in fact, it's not supported anymore as of Spring Framework 5.0, see SPR-13499.

Spring mvc cannot find CacheInterceptor

I'm exploring cache in spring mvc framework. Following the guide on internet like here or here, i'm configuring:
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
... other configuration are omitted....
</beans>
Before adding configuration for cache, my web app run normally, now, my web app start with error:
java.lang.NoClassDefFoundError: org/springframework/cache/interceptor/CacheInterceptor
Not sure what i did wrong?
A NoClassDefFoundError is simply an exception that is thrown when java can't find a certain class that another library uses.
All this means is that when it gets thrown you just need to get it on the classpath. You can make sure it's there by going to libraries-> maven dependencies(if you are using maven) -> and then go to the location of the noclassdef that is being thrown, in your case it would be at org/springframework/cache/interceptor/CacheInterceptor, this is just so you know why you are getting this error, you should not see it now.
As for how to solve it, this could be happening because of 3 things:
Your maven dependencies are not on the deployment assemblies(libraries are in the project, but not on the server), although probably spring wouldn't work at all if this was the case.
You don't have the dependency, add this to pom.xml:
org.springframework
spring-context
4.1.4.RELEASE
You have conflicting spring versions, make all version tags the same.

no declaration can be found for element 'tx:annotation-driven'

I am using spring 4.1.1 for my project,i am getting the following exception. Given below are the definitions i am using , need help!
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"
Make sure that you have the spring-tx dependency on your classpath. If you are using maven add the dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
If you already have the dependency check the version as it might not be 4.1.
You can also declare your namespace omitting the version so that it always picks the latest
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
Specify the version if you really need some feature specific to the version

Categories