This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Caching in java using Coherence

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I am trying to build a simple caching application using coherence. I am able to bring up the service and push or pop value from the cache via command line (cmd). But when I try to do the same using Java code, I am getting the following error:
null
Exception in thread "main" java.lang.NullPointerException
at com.learn.coherence.main.TestCacheMainOne.main(TestCacheMainOne.java:14)

The code is fairly simple and small. I am marking the line causing this exception in bold.

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class TestCacheMainOne {
public static void main(String[] args) {
NamedCache cache = CacheFactory.getCache("VirtualCache");
System.out.println(cache);
String key = "key1";
String value = "value1";
cache.put(key, value);
String receivedValue = (String) cache.get(key);
System.out.println(receivedValue);
}
}

Please help me regarding this. I have a very stringent deadline to beat. Kindly help.
 
Sheriff
Posts: 21773
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That highlighted line can't be the actual source of the exception. Either line 14 is a different line, or you're omitting parts of the stack trace.

Also, please EaseUp. I know all about deadlines, but rushing people will usually not help you out any quicker.
 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again Rob.
Yeah you are correct, I screwed up the line numbers while trying to compact the code. I have made a new post with some more details and proper information here: http://www.coderanch.com/t/656371/java/java/NPE-caching-coherence-java#3039681
Can you please check this out? Thanks in advance.
 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
My other thread was locked so I have to paste the rectified information here as a comment. Please check this out.
Value of the NamedCache variable cache after calling getCache(): null
Exception in thread "main" java.lang.NullPointerException
at com.learn.coherence.main.TestCacheMainOne.main(TestCacheMainOne.java:9)

The code is fairly simple and small. I am marking the line causing this exception in bold.
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class TestCacheMainOne {
public static void main(String[] args) {
NamedCache cache = CacheFactory.getCache("VirtualCache");
System.out.println("Value of the NamedCache variable cache after calling getCache(): " + cache);
String key = "key1";
String value = "value1";
cache.put(key, value);
String receivedValue = (String) cache.get(key);
System.out.println(receivedValue);
}
}
 
Rob Spoor
Sheriff
Posts: 21773
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently CacheFactory.getCache returns null. Do you have any Coherence configuration? Can you show that?
 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rob,
I have a config file named coherence-cache-config.xml in my /src/main/resources classpath folder
The contents are as follows:
<?xml version="1.0"?>
<!DOCTYPE cache-config SYSTEM "cache-config.dtd">

<cache-config>
<caching-scheme-mapping>
<!-- Caches with any name will be created as default replicated. -->
<cache-mapping>
<cache-name>*</cache-name>
<scheme-name>default-replicated</scheme-name>
</cache-mapping>
<cache-mapping>
<cache-name>VirtualCache</cache-name>
<scheme-name>default-distributed</scheme-name>
</cache-mapping>
</caching-scheme-mapping>

<caching-schemes>
<!-- Default Replicated caching scheme. -->
<replicated-scheme>
<scheme-name>default-replicated</scheme-name>
<service-name>ReplicatedCache</service-name>

<backing-map-scheme>
<class-scheme>
<scheme-ref>default-backing-map</scheme-ref>
</class-scheme>
</backing-map-scheme>
</replicated-scheme>

<!-- Default Distributed caching scheme. -->
<distributed-scheme>
<scheme-name>default-distributed</scheme-name>
<service-name>DistributedCache</service-name>

<backing-map-scheme>
<class-scheme>
<scheme-ref>default-backing-map</scheme-ref>
</class-scheme>
</backing-map-scheme>
</distributed-scheme>

<!-- Default backing map scheme definition used by all The caches that
do not require any eviction policies -->
<class-scheme>
<scheme-name>default-backing-map</scheme-name>

<class-name>com.tangosol.util.SafeHashMap</class-name>
</class-scheme>

</caching-schemes>
</cache-config>
 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I also tried with a different xml config but still got the same result. I am pasting the config here:
<?xml version="1.0"?>
<!DOCTYPE cache-config SYSTEM "cache-config.dtd">

<cache-config>
<defaults>
<serializers>pof</serializers>
</defaults>
<caching-scheme-mapping>
<!-- Caches with any name will be created as default replicated. -->
<cache-mapping>
<cache-name>VirtualCache</cache-name>
<scheme-name>MyNearScheme</scheme-name>
</cache-mapping>

</caching-scheme-mapping>

<caching-schemes>
<near-scheme>
<scheme-name>MyNearScheme</scheme-name>

<front-scheme>
<local-scheme>
<eviction-policy>LRU</eviction-policy>
<high-units>5</high-units>
<unit-calculator>FIXED</unit-calculator>
<expiry-delay>1h</expiry-delay>
</local-scheme>
</front-scheme>

<back-scheme>
<distributed-scheme>
<scheme-name>near-distributed</scheme-name>
<backing-map-scheme>
<local-scheme>
<high-units>100</high-units>
<unit-calculator>FIXED</unit-calculator>
</local-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
</back-scheme>

<invalidation-strategy>present</invalidation-strategy>

</near-scheme>
</caching-schemes>
</cache-config>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!