简体   繁体   中英

How to get a Hazelcast map inside a Camel route?

I'm really fresh to Camel (and hazelcast, at that), and I've been playing around with it a bit recently. A seemingly simple operation is causing me a lot of trouble, and I'm struggling to find any waypointers anywhere.

I have a listener watching for changes on a hazelcast map. If said changes match a certain criteria, I want to grab the entire map and send it to a processor. Something like this:

from(hazelcast:map:someMap?hazelcastInstance=#hazelcastInstance)
.filter().method(SomeFilter.class, filterMethod)
.???
// If the filter cafeterias are met, get the entire map and send it to a processor

But I am really not sure how to, well, get entire map itself, especially using Java DSL. The closest thing I've found is to get the map's keySet and then call getAll(keySet) on it, which seems needlessly contrived for such a simple thing? If this is really the preferred method, there is another issue - how do you pass said keySet as the parameter to the getAll operation? Ie:

<snip>
.setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_KEYS_OPERATION))
.to(hazelcast:map:someMap?hazelcastInstance=#hazelcastInstance) // Gets the keySet just fine.
.setHeader(HazelcastConstants.OPERATION, constants(HazelcastConstants.GET_ALL_OPERATION))
.????
// I've tried .setHeader(HazelcastConstants.OPERATION_PARAM, new 
// SimpleExpression("${body}")> here,
// amongst many other things, but, I just get an empty object back, so it's
// pretty clear I'm messing up 
// the format or parameter choice here.
.to(hazelcast:map:someMap?hazelcastInstance=#hazelcastInstance)

Using camel 2.18.0 here, by the way.

Well after bit more trial and error, I got it working. I swear it was one of the first things I tried, but I must've bungled it up on the first attempt and got stumped. All you really do is get the keySet and then put into OBJECT_ID. So something like this:

.setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_KEYS_OPERATION))
.to(hazelcast:map:someMap?hazelcastInstance=#hazelcastInstance)
.setHeader(HazelcastConstants.OPERATION, constants(HazelcastConstants.GET_ALL_OPERATION))
.setHeader(HazelcastConstants.OBJECT_ID, new SimpleExpression("${body}"))
.to(hazelcast:map:someMap?hazelcastInstance=#hazelcastInstance)

(Note: it's hazelcast-map in newer versions.)

And ta-dah, you've got a Hashmap to work with. The alternative seems to use QUERY, but I couldn't figure out the proper syntax for, well, the query. I'd love to hear if anyone has a working example with a QUERY header...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM