[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Problem populating ehCache with JSON Object via HttpClient using POST  XML
Forum Index -> Ehcache
Author Message
jdekker

journeyman

Joined: 11/30/2012 16:20:21
Messages: 21
Offline

Am using Java 6, Tomcat 7, Jersey, HttpClient, with ehCache Server.

Registered a new cache by the name of ipad inside $CATALINA_HOME/ehcache/WEB-INF/classes/ehcache.xml
Code:
  <cache name="ipad"
            maxElementsInMemory="10000"
            eternal="true"
            overflowToDisk="false"
            memoryStoreEvictionPolicy="FIFO"
 />
 


This is what my Client looks like:

Code:
 import org.apache.http.impl.client.DefaultHttpClient;
 
 public class EhCachePostClient {
 	public static void main(String[] args) throws Exception {	
 			String jsonString = "{\"qty\":100,\"name\":\"iPad 4\"}";
 			
 			// Post the same object to ehCache
 			DefaultHttpClient ehCacheClient = new DefaultHttpClient();
 			String ehCacheResponseString = 
 			       EhCacheClientHelper.getEhCacheJsonResponseString(ehCacheClient, jsonString);
 			System.out.println("\nFrom ehCache Server: " + ehCacheResponseString);
 			ehCacheClient.getConnectionManager().shutdown();
 	}
 }
 


Here's the EhCacheClientHelper class (where majority of the work is done):

Code:
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 
 public class EhCacheClientHelper {
 
 	private static final String EHCACHE_IPAD_URI 
 	               = "http://localhost:8080/ehcache/rest/ipad";
 
 	public static String getEhCacheJsonResponseString(DefaultHttpClient httpClient, 
 	                                                  String responseString) 
 	throws Exception {
 		HttpPost postRequest = new HttpPost(EHCACHE_MDS_URI);
 
 		StringEntity input = new StringEntity(responseString);
 		input.setContentType("application/json");
 		postRequest.setEntity(input);
 
 		HttpResponse response = httpClient.execute(postRequest);
 
 		if (response.getStatusLine().getStatusCode() != 201) {
 			throw new RuntimeException("Failed : HTTP error code : " 
 			         + response.getStatusLine().getStatusCode());
 		}
 
 		BufferedReader br = new BufferedReader(
 		       new InputStreamReader((response.getEntity().getContent())));
 
 		String output;
 		StringBuilder ehCacheOutput = new StringBuilder();
 		System.out.println("\nOutput from ehCache Server .... \n");
 		while ((output = br.readLine()) != null) {
 			ehCacheOutput.append(output);
 		}
 		return ehCacheOutput.toString();
 	}
 }
 


This is the exception I get:

Code:
 Exception in thread "main" java.lang.IllegalStateException: Target host must not be null, or set in parameters.
 	at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:784)
 	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:414)
 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
 	at com.myapp.client.EhCacheClientHelper.getEhCacheJsonResponseString(EhCacheClientHelper.java:22)
 	at com.myapp.client.EhCachePostClient.main(EhCachePostClient.java:19)
 


What am I doing wrong?

All I want to do is create a sample cache in ehCache, post some type of JSON object to it, and then retrieve it using curl command.

Is this a hard thing to do? What I am missing (in terms of implementation, configuration, etc.)?

Thanks for taking the time to read this...
klalithr

consul

Joined: 01/23/2011 10:58:07
Messages: 485
Offline

On a different note, Why dont you use the OSS Terracotta Server and simply use any framework (axis, jaxb) to expose your cache as a service?
This would provide additional scale and HA options.

Karthik Lalithraj (Terracotta)
jdekker

journeyman

Joined: 11/30/2012 16:20:21
Messages: 21
Offline

klalithr,

Because I just want to use the ehCache to do it... I got ehCache working with REST. I don't care about scalability.

I really don't understand why no one was able to answer this question (how can I post JSON from a Restful Web Service to ehCache)? If posting JSON to ehCache from a RESTful Web Service is too difficult to answer, than I might have to drop ehCache from the requirements list and look at other solutions (memcache and / or Google's solution).

Please answer the question at hand and stop suggesting other alternatives like the Terracotta Server.

Peacefully,

James
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team