Introduction to JCache JSR 107 Part 2 sharing objects in a cache between jar files

From Resin 4.0 Wiki

Jump to: navigation, search

In progress....



public class HelloObject implements Serializable {
	private String message;
	public HelloObject (String message) {
		this.message = message;

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;


Generate hello.jar

Share between two war files based on last example.

It does not work at first.

Webapps, virtual hosts have a name space to avoid cache key collisions.

Use Injections and XML config to configure shared cache.

    <resin:ClusterCache name="t1">

package hello.world2;



import javax.cache.Cache;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet implementation class HelloServlet
public class HelloServlet extends HttpServlet {
	@Inject Cache<String,HelloObject> cache;
	public Cache<String, HelloObject> cache() {
		return cache;

	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("<html><head>2</head><body><h1>jchache 2</h1><p>");

		HelloObject hello = cache().get("hello message");
		if (hello == null) {
			String helloMessage = new StringBuilder(20)
				.append("Hello World ! <br />")
			hello = new HelloObject(helloMessage);
			cache().put("hello message", hello);




Explain classloader issue...

Explain one way to fix it for a shared cache. Explain by value and by reference.

  <cluster id="app">
    <!-- define the servers in the cluster -->
    <server-multi address-list="${app_servers}" id-prefix="app-" port="6800"/>

    <resin:ClusterCache name="t1">

    		<tree-loader path="${resin.root}/myjars"/>

Show the above but how to do it for a virtual host. Show the above again but how to fix it with by value instead of by reference ala JCache.

Take explanation out of emails where you explained it to customer.

Show how to add custom serializer to handle cache serialization so objects don't have to follow Serialization rules.

Personal tools