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

From Resin 4.0 Wiki

Revision as of 17:00, 23 January 2013 by Rick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In progress....


package hello.world.helloobject;

import java.io.Serializable;

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">
  	</resin:ClusterCache>

package hello.world2;


import hello.world.helloobject.HelloObject;

import java.io.IOException;

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
 */
@WebServlet("/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.setContentType("text/html");
		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 />")
				.append(System.currentTimeMillis()).toString();
			hello = new HelloObject(helloMessage);
			cache().put("hello message", hello);
		}

		response.getWriter().append(hello.getMessage());
		

		response.getWriter().append("</p></body></html>");
	}

}

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">
  	</resin:ClusterCache>

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

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
TOOLBOX
LANGUAGES