How to share an object between methods on differen

2019-06-05 22:40发布

I am search the web without luck about something I thought would be simple but apparently it is not.
All I want to do is to do, is create a HashSet in a method that is called in a camel route and then pass this HashSet to a method in another camel route.
What my search returned is that I should use a cache but I can't find any example (a simple one) that will show me how implement this.
Method "findProperties" in first route creates a HashSet which I want to use in the second route in "parseFile" method.

from("file:{{List}}?noop=true")
.autoStartup(true)
.unmarshal().csv()
.to("bean:ParserUtils?method=findProperties")
.end();


from("file:{{Path}}?move={{processedPath}}")
.autoStartup(true)
.unmarshal().csv()
.to("bean:Parser?method=parseFile")
.end()

I would really appreciate a simple example of getting and setting an object in cache or another solution maybe.

1条回答
萌系小妹纸
2楼-- · 2019-06-05 23:11

since your first route doesn't invoke your second route, there is no messaging between them to pass data around...so yes, you need to use some external means to access data that is shared between routes/threads...

this can be as simple as a class/static level variable in your ParserUtils instance or using camel-cache (ehcache, etc), camel-hazelcast, etc...the choice is yours

here are some examples using camel-cache...

https://svn.apache.org/repos/asf/camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java

查看更多
登录 后发表回答