Grails 2.1.1 with Terracotta and Ehcache integration - ehcache

I'm trying to integrate Grails 2.1.1 with Terracotta and Ehcache but I'm not getting any good results whatsoever.
Could anybody give me some advice in order to configure it? I'm a little bit confused.

Working on the subj now.
First create ehcache.xml in your grails-app/conf directory. It should look like that:
<ehcache>
<terracottaConfig url="vm4:9510"/>
<defaultCache
maxElementsInMemory="50"
eternal="false"
timeToIdleSeconds="20"
timeToLiveSeconds="20"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="processedUrlCache"
maxElementsInMemory="50000"
eternal="true">
<terracotta />
</cache>
</ehcache>
Consider that terracottaConfig url should point to your actual host:port where Terracotta server lives. Make sure that your tarracotta cache node has <terracotta/> subnode (similarly to my processUrlCache entry).
Now, put these 3 jars into your project lib directory:
ehcache-core-ee-2.6.2.jar, ehcache-terracotta-ee-2.6.2.jar, terracotta-toolkit-1.6-runtime-ee-5.2.0.jar
Actual version may differ (but it should be exactly the same as on your terracotta server, so my suggestion is to take those jars from server lib directories).
Now, run your server (start-tc-server.sh or start-tc-server.bat depending on Unix/Windows platform) and run your grails app. For the first start I would suggest you enabling debug logs in your Config.groovy file by putting
debug 'net.sf.ehcache'
into your log4j section.
Let me know would it help you.

Related

How to Configure Eclipse to Work with `ehcache`

Summary/Question
I've rolled onto a project that uses ehcache. The project is maven enabled and when I run mvn clean install from cli all ends well.
The project can also be opened in NetBeans and everything displays properly, however when I open the project in eclipse, I'm seeing some errors related to ehcache. In NetBeans the spring-cache.xml is located under a "Web Pages" folder. This made me think that the eclipse project may not be recognized as a dynamic web project, however following the directions here: https://www.mkyong.com/java/how-to-convert-java-project-to-web-project-in-eclipse/ I was able to verify that the project is indeed setup to support Dynamic Web Module, Java and JavaScript.
I also opened the project in IntelliJ and also did not see any issues with the spring-cache.xml.
I also didn't find documentation related to this version of ehcache on http://www.ehcache.org/documentation/ but the fact that things work in NetBeans, IntelliJ and via mvn clean install make me think its my eclipse setup. Thoughts?
More Details:
The errors I see in eclipse are:
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ehcache:annotation-driven'.
- schema_reference.4: Failed to read schema document 'http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring-1.1.xsd, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
And
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ehcache:config'.
My maven dependency looks like:
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.2.0</version>
</dependency>
Which matches what it should be, based on: https://mvnrepository.com/artifact/com.googlecode.ehcache-spring-annotations/ehcache-spring-annotations/1.2.0
My spring-cache.xml file 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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-driven />
<ehcache:config cache-manager="cacheManager">
<ehcache:evict-expired-elements interval="60" />
</ehcache:config>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="${ehcache.config.location}"/>
</bean>
</beans>
Looks like current versions of NetBeans and IntelliJ aren't as strict on checking things as eclipse is. :) The following is what I found out.
From ehcache with Spring. google code xsd file not found I found out that the top error was related to the fact that the xsd is no longer hosted at the url provided.
Doing some googles, I found a reference to the version on github at: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.2.xsd by changing my bottom xsi schemalocation to the above url, it resolved the issue in eclipse.
The stack overflow questions referenced, also provides a link to the google code archive of: https://code.google.com/archive/p/ehcache-spring-annotations/source/default/source
If you download the zip, you can then find the offical xsd in the zip at: /schema/ehcache-spring/ehcache-spring-1.2.xsd (it also has ehcache-spring-1.0.xsd and ehcache-spring1.1.xsd in case anyone else has issues with even earlier versions).
I found a way to reference the local schema file here: How to reference a local XML Schema file correctly?
I'm going to post a question on how to reference it from a relative path of the project, as we have both windows and mac developers on this project. If this is of use to you, the details will be here: How to Reference Local XSD File By Relative Path
Summary of issue:
xsi-schemaLocation reference:
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
results in validation-issues in eclipse, on commandline maven there are no issues.
replacing the ref with:
https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
solves issue in eclipse. In maven the saxparser will complain however if you 're not connected to internet.
By putting a proxy ref :
-Dhttp.proxyHost=[proxyhost] -Dhttp.proxyPort=[proxyport] -Dhttps.proxyHost=[proxyhost] -Dhttps.proxyPort=[proxyport]
the maven build succeeds....
I investigated this difference in behaviour:
in the ehcache-spring jar, ehcache-spring-annotations-1.1.2.jar,
a META-INF/spring.schemas file is present.
Content of that file:
http\://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.0.xsd=com/googlecode/ehcache/annotations/ehcache-spring-1.0.xsd
http\://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd=com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
cmdline maven uses this content (and finds the xsd in the jar).
eclipse does not.
Solution I used:
go to eclipse preferences,
XML -> XML Catalog and add a new user entry:
Location: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
Key type: Public id
Key: http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
Alternative web adress: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
Re-validating spring context files solved issues in my eclipse workspace.
I like this solution better because it is a shortcoming of eclipse which i solve with an eclipse-specific solution.

Copy compiled jar into %TOMCAT%/lib automatically using Maven

I would like to know if there is a good way to build JAR files with Maven and after that copy them into %TOMCAT%/lib folder.
What I want to achieve is that every time when I deploy my WAR in the cloud (using Jenkins and Cloud Foundry), I get all JARs needed by Tomcat copied in its lib folder.
My use case is, my application uses Redis to store sessions (to be cloud ready), and I have added this configuration to %TOMCAT%/context.xml file
<!-- session persistence is handled by Redis Data Server -->
<Valve className="com.gopivotal.manager.SessionFlushValve" />
<Manager className="org.apache.catalina.session.PersistentManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60">
<Store className="com.gopivotal.manager.redis.RedisStore" />
</Manager>
Sessions are stored like a charm when I copy manually redis.jar, commons-pool.jar and jedis.jar in %TOMCAT%/lib, but I want to do that automatically based on the best practice.

How can I deploy a web application to tomcat application server via Terminal command

I am currently testing a large number of webservices. I would like to deploy and undeploy to tomcat application server via terminal command as fast as I can. Using the HTML GUI would not be reasonable for the large number of webservices that I need to deploy. Can anyone assist me, in how to deploy via a terminal command?
Furthermore, I am writing a ash script that automates the deployment process, so perhaps if someone can give me some some direction it would be great.
Ideally, I am looking to do something like this on the command line:
TOMCAT --parameter Specify path to WAR file --parameter2 --specify some sort of config file
First you need to make sure that tomcat-user.xml is configured with the correct users and roles. The minimum role configuration is "admin,manager-script":
Find out where tomcat is installed. You can do this in bash: catalina version
Navigate to the CATALINA_HOME config directory which is displayed in the output from the previous command.
cd /usr/local/Cellar/tomcat/8.0.22/libexec/conf
Note: In my example, I used homebrew to install tomcat 8, replace this path with whatever is displayed in your command line output.
Verify that the server.xml contains a UserDatabase resource (if it doesn't add it):
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
By default tomcat uses an in-memory database to store users and roles. This is configured in the conf/server.xml. And delegates user & role declaration to the conf/tomcat-users.xml file. See: http://tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#UserDatabaseRealm for more information.
Then verify that config/user-tomcat.xml exists and looks like this:
<tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<role rolename="admin"/>
<role rolename="manager-script"/>
<user username="admin" roles="admin,manager-script" password="admin" />
</tomcat-users>
Now, you're ready to deploy a war!
Here are two ways to do that...
Using wget:
wget is a nifty tool that lets you do http requests via the command line. I recommend installing it using a package manager like homebrew, otherwise, you can install using this wget install guide.
First restart your server to pick up any configuration changes that you may have done. catalina stop, then catalina start, will do the trick from bash.
*Note: When using wget to start deploy a war to tomcat, tomcat needs to be started first.
Now execute the following command:
wget --http-user=admin --http-password=admin "http://localhost:8080/manager/text/deploy?war=file:/Users/yourusername/app/target/APP-1.0.0.war&path=/app"
Now go to http://localhost:8080/app/ to see your webapp.
Notes:
Wait a few seconds to let the http request to send and fully deploy the war. (sometimes this takes a while).
You may be tempted to reference the war file using the home directory shortcut like this file:~/app/target/APP-1.0.0.war, but that won't work.
To undeploy the war simply replace deploy with undeploy in the wget command.
Using the Tomcat Maven Plugin:
If you have the source code, you can build and deploy the war yourself very easily with the tomcat7-maven-plugin. Note: At the time I wrote this there was no tomcat8-maven-plugin; the tomcat-7-maven-plugin works just fine for tomcat 8.
Add the plugin to the pom.xml:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${version.tomcat.maven.plugin}</version>
<configuration>
<path>/${project.build.finalName}</path>
<configurationDir>${env.CATALINA_HOME}</configurationDir>
<additionalConfigFilesDir>${env.CATALINA_HOME}/conf</additionalConfigFilesDir>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
</configuration>
</plugin>
Navigate to your project root, where the pom.xml is, and run the following command: mvn tomcat7:run
To deploy a WAR from the command line you can use wget (or curl)
wget "http://localhost:8080/manager/text/deploy?war=file:/path/to/MyWar.war&path=/MyApp" -O -
Take a look at Tomcat's manager webapp. You can use the "text" interface to do things from the command-line. Tomcat even comes with some Apache Ant tasks that can deploy, undeploy, etc. for you.

Same webapp on Tomcat with different log4j config for each

I've hunted and read, and I think there's no way...but just in case.
I need to deploy the same webapp to Tomcat multiple times, each with a different config that indicates which database to work with. I've figured out how to do that without modifying the contents of the war file.
In short, I copied WebApp.jar to WebAppDB1.jar, WebAppDB2.jar, and deploy these to the webapps folder, and use a custom context configuration for each in tomcat/conf/Catalina/localhost. So I'm good there.
But I would really like for each of these to log to a separate file. As it is, everything goes to catalina.out. It's a Spring app using log4j and slf4j. Every avenue I've explored gets me nowhere.
For now, I'm back to updating the actual war file, going into WEB-INF/classes and updating log4j.xml, but that makes it a manual process.
Any ideas?
You can put the log4j.xml files somewhere in the outside the webapp and use Tomcat's VirtualWebappLoader to load different log4.xml for each webapps.
http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation
It looks like this.
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB1.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB1" />
</Context>
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB2.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB2" />
</Context>
And then put log4j.xml files into /somewhere/WebAppDB1 and /somewhere/WebAppDB2.
Hope this helps.

How To Setup Logback with Tomcat 6 on Windows

I have tomcat6 installed on my Windows desktop for testing purpose. At the moment nothing is added to the webapps folder, just the normal tomcat start up. Following log files are produced:
catalina.2011-12-24.log
host-manager.2011-12-24.log
jakarta_service_20111224.log
localhost.2011-12-24.log
manager.2011-12-24.log
stderr_20111224.log
stdout_20111224.log
My problem is simple, I want these logs [stdout included] to be rolled and compressed on the basis of defined size limit.
I have read about logback and its capabilities to roll and compress files with defined policies, but I have not been able to make it work for me. I have copied following jars in $TOMCAT_HOME/lib directory:
logback-access-1.0.0.jar
logback-classic-1.0.0.jar
logback-core-1.0.0.jar
Some sites suggested these libs should be added as well:
jul-to-slf4j-1.6.4.jar
slf4j-api-1.6.4.jar
I have tried copying logback.xml in $TOMCAT_HOME/conf and $TOMCAT_HOME/lib, but unable to make it work.
I would really appreciate if someone can help me with this, best could provide me with a logback.xml and jars/xml location where these files should be copied to make it work.
P.S. I have not deployed any webapp in the tomcat, it just the basic tomcat setup.
I think this tutorial can help. Tomcat 6 works only with log....0.9.30.jar or lower. Tomcat + SLF4J + Logback

Resources