gpt4 book ai didi

apache-camel - 如何在 Camel 上下文之外访问 Apache camel eh-cache?

转载 作者:行者123 更新时间:2023-12-04 05:13:13 30 4
gpt4 key购买 nike

我想使用来自网络服务的数据并将其放入 Camel eh-cache 中。后来我想通过 CacheManager 在 Camel 上下文之外使用这个缓存。我没有找到任何方法。

在下面的代码中,我跳过了 Web 服务的使用,并使用了 Map 中的数据并将其提供给 eh-cache,但我无法使用 CacheManager 访问此缓存。

CamelRouter 类

package com.camel;


import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.ehcache.EhcacheConstants;
import org.apache.camel.main.Main;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Configuration;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;

public class Test {

private static Main main;

public static void main(String[] args) throws Exception {

main = new Main();
main.addRouteBuilder(new RouteBuilder() {

@Override
public void configure() throws Exception {
from("timer:foo?period=5s&repeatCount=1")
.process(exchange ->{
Map<String, String> inputMap = new HashMap<>();
inputMap.put("name", "murli");
inputMap.put("lastname", "hiware");
inputMap.put("city", "pune");
exchange.getIn().setBody(inputMap);
exchange.getIn().setHeader("CamelEhcacheAction", EhcacheConstants.ACTION_PUT_ALL);
})
.to("ehcache://testCache?configUri=ehcache.xml&keyType=java.lang.String&valueType=java.lang.String")
.process(exchange -> {
URL myUrl = getClass().getResource("/ehcache.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
myCacheManager.init();
//here I want to access already created testCache component but it is creating new one.
Cache<String, String> cache = myCacheManager.getCache("testCache", String.class, String.class);
System.out.println("Cache Element:"+cache.get("name"));
System.out.println("Exchange Message:"+exchange.getIn().getBody());

});
}
});

main.run();
}

}

ehcache 配置文件

<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

<cache alias="testCache">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache>

<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>

<cache alias="bar" uses-template="myDefaults">
<key-type>java.lang.Number</key-type>
</cache>

<cache alias="simpleCache" uses-template="myDefaults" />

</config>

请让我知道我尝试实现的用例是否可以使用 camel eh-cache 实现?

最佳答案

您通常应该能够使用 from("ehcache://...) 检索值。

但是,我假设您确实想要访问缓存或缓存管理器。

这是这样做的方法:

public static void main(String[] args) throws Exception {
URL url = App.class.getResource("/ehcache.xml");
Configuration xmlConfig = new XmlConfiguration(url);

CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
cacheManager.init();

Main main = new Main();
main.bind("cacheManager", cacheManager);

main.addRouteBuilder(new RouteBuilder() {

@Override
public void configure() throws Exception {
from("timer:foo?period=5s&repeatCount=1")
.process(exchange ->{
Map<String, String> inputMap = new HashMap<>();
inputMap.put("name", "murli");
inputMap.put("lastname", "hiware");
inputMap.put("city", "pune");
exchange.getIn().setBody(inputMap);
exchange.getIn().setHeader("CamelEhcacheAction", EhcacheConstants.ACTION_PUT_ALL);
})
.to("ehcache://testCache?cacheManager=#cacheManager&keyType=java.lang.String&valueType=java.lang.String")
.process(exchange -> {
//here I want to access already created testCache component but it is creating new one.
Cache<String, String> cache = cacheManager.getCache("testCache", String.class, String.class);
System.out.println("Cache Element:"+cache.get("name"));
System.out.println("Exchange Message:"+exchange.getIn().getBody());

});
}
});

main.run();
}

关于apache-camel - 如何在 Camel 上下文之外访问 Apache camel eh-cache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43915633/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com