• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

I want to get whatever i put into element in parent project that same element i want to get tit from child project

I want to get whatever i put into element in parent project that same element i want to get tit from child project

package com.sm;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;

import com.sm.CacheVO;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

/**
 * A simple example that demonstrates how to obtain access to a cache, insert objects in the cache,
 * and retrieve them back out.
 */
public class CacheMnagerImpl implements Serializable
{
    /**
     * The CacheManager provides us access to individual Cache instances
     */
    private static final CacheManager cacheManager = new CacheManager();

    /**
     * A cache that we're designating to hold Widget instances
     */
    private static Ehcache widgetCache;

    public CacheMnagerImpl()
    {
        // Load our widgets cache:
          try
          {
          InputStream fis =
              new FileInputStream(new File("D://ehcache/ehcache.xml"));
           CacheManager manager = new CacheManager(fis);
            widgetCache = manager.getEhcache( "widgets" );
        //widgetCache = cacheManager.getEhcache( "widgets" );
          }
          catch(Exception e){e.printStackTrace();}
    }

    /**
     * Adds a new Widget to the Cache
     *
     * @param id        The ID of the widget
     * @param widget    The Widget itself
     */
    public void addWidget( String id, String value )
    {
        // Create an EHCache Element to hold the widget
          //widget.setName("onetwo");
        Element element = new Element( id, value );

        // Add the element to the cache
        widgetCache.put( element );
    }

    /**
     * Retrieves a Widget from the cache
     *
     * @param id        The ID of the Widget to retrieve
     * @return          The requested Widget or null if the Widget is not in the cache
     */
    public String getWidget( String id )
    {
        // Retrieve the element that contains the requested widget
        Element element = widgetCache.get( id );
        if( element != null )
        {
            // Get the value out of the element and cast it to a Widget
            return (String) element.getValue();
        }

        // We don't have the object in the cache so return null
        return null;
    }
   
    public static void main(String s[]){
          
          CacheMnagerImpl mnager =new CacheMnagerImpl();
          
          mnager.addWidget("one", "one");
          System.out.println(mnager.getWidget("one"));
          
    }
}

this parent project i will get the out put one

child project code
package com.sm;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

import java.io.*;
/**
* A simple example Java client which uses the built-in java.net.URLConnection.
*
* @author BryantR
* @author Greg Luck
*/
public class ehCacheClientParentCall{
private static String TABLE_COLUMN_BASE =
       "http://192.168.1.101:8080/Parent/ehcache.xml";
private static String TABLE_COLUMN_ELEMENT =
       "http://192.168.1.101:8080/Parent/ehcache.xml";
private static Ehcache widgetCache;
/**
* Creates a new instance of EHCacheREST
*/
public ehCacheClientParentCall() {
}
public static void main(String[] args) throws IOException {
//      URL url = getClass().getResource("/anotherconfigurationname.xml");
//      CacheManager manager = CacheManager.newInstance(url);
      InputStream fis =
          new FileInputStream(new File("D://ehcache/ehcache.xml"));
       CacheManager manager = new CacheManager(fis);
        widgetCache = manager.getEhcache( "widgets" );
        Element element = widgetCache.get( "one" );
       // System.out.println((String) element.getValue());
      if( element != null )
      {
          // Get the value out of the element and cast it to a Widget
         System.out.println((String) element.getValue());
      }

}
}
this child project i didnt get any output

what i want to do access an element from parent cache project from child project code

please help me
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
1 Solution
 
CEHJCommented:
I don't use ehcache but it seems to me that the issue of parent/child projects isn't the thing of prime importance. All that's necessary probably is to have an instance of the cache implementation running before you try to use the cache. How you arrange that is up to you. The most direct way would be to call
CacheMnagerImpl.main(null); // (strange typo)

Open in new window

as the first line of your client's main
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
I didnt get any answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now