Traveling Java 8 Lambdas With Headlands / JCache / Hazelcast

Headlands is an Apache-licensed RESTful endpoint to the JCache API. The "Traveling Lambdas" release comes with thread pool monitoring and support for JavaScript (Nashorn) Entry- and Cache-Processors.

Cache processors are able to perform operation on the whole cache:


curl -i -XPUT -d'duke' http://localhost:8080/headlands/resources/caches/workshops/entries/chief

curl -i -H'Content-type:application/json' -XPOST --data 'function process(cache, result) { 
    for each (entry in cache) { 
        var key = entry.key; 
        var value = entry.value; 
        print(key, "=", value); 
        result.put(key, value+" result"); 
    } 
    return result; 
}' http://localhost:8080/headlands/resources/cache-processors/workshops

An entry processor only operates on entries with passed keys:


curl -i -H'Content-type:application/json' -XPOST --data '{
  "script" : "function process(entry, args) {return \"The answer: \" + entry.getValue();}",
  "keys" : [
    "chief"
  ]
}' http://localhost:8080/headlands/resources/entry-processors/workshops

With entry- and cache-processors written in JavaScript only String-instances travel over the wire but are materialized on the other serverside as Java instances. Functional Java 8 interfaces are serialized into a String instead of the default Java serialization.

Checkout the entire curl walk-through.

Headlands ships with hazelcast (just launch multiple instances to get an in-memory grid) and was tested on Payara (Java EE 7 + Java8)

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: effectivejavaee.com.

Comments:

Hi, had a look at headlands. So from that code I do this:
HazelcastInstance client = Hazelcast.newHazelcastInstance();

CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
Cache<String, String> cache = cacheManager.getCache("test", String.class, String.class);
if (cache == null) {
CompleteConfiguration<String, String> config
= new MutableConfiguration<String, String>()
.setTypes(String.class, String.class);
cache = cacheManager.createCache("test", config);
String json = Json.createObjectBuilder().add("name", "kalle").build().toString();
cache.put("kalle", json);
}

String kalle = cache.get("kalle");
ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("javascript");
Invocable invocable = (Invocable) nashorn;
String script = "function process(entry, args) { print('invoked..');return \"works\";}";
// + "function getBackupProcessor() { return null;} ";
Object eval = nashorn.eval(script);
EntryProcessor<String, String, String> ep = invocable.getInterface(javax.cache.processor.EntryProcessor.class);
cache.invoke(kalle, ep, new Object[]{});

But I get an exception, how do I avoid it to happen? And how does your code work?

xception in thread "main" javax.cache.processor.EntryProcessorException: com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable serializer for class javax.cache.processor.EntryProcessor$$NashornJavaAdapter

Posted by Peter on November 10, 2015 at 07:33 PM CET #

'lunch' should be 'launch' :)

Posted by The Alchemist on November 13, 2015 at 02:12 PM CET #

@Alchemist,

absolutely :-) -> fixed!,

cheers,

adam

Posted by 47.68.39.186 on November 16, 2015 at 10:13 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License