sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-04 05:13:13 31 4
mua khóa gpt4 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() {

@Ghi đè
public void configure() throws Exception {
from("timer:foo?period=5s&repeatCount=1")
.process(exchange ->{
Map 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 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 配置文件

<>
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">


java.lang.String
java.lang.String

2000
100




java.lang.Long
java.lang.String
200



java.lang.Number





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

1 Câu trả lời

您通常应该能够使用 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() {

@Ghi đè
public void configure() throws Exception {
from("timer:foo?period=5s&repeatCount=1")
.process(exchange ->{
Map 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 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/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com