简体   繁体   English

在Camel Spring-Boot微服务中实现静态缓存?

[英]Implementing static caching in a Camel Spring-Boot microservice?

I have a Spring-boot/Camel microservice that accepts XML via a Restful POST with various header parms (1), transforms the XML via a static xQuery file stored on the classpath (2), then based on vales of the result retrieve a set of XSL templates from a relational database using SQL(3). 我有一个Spring-boot / Camel微服务,它通过带有各种标头参数的Restful POST接受XML(1),通过存储在类路径中的静态xQuery文件转换XML(2),然后根据结果的值检索一组SQL(3)从关系数据库中提取XSL模板。 These templates are themselves then transformed and assembled using xQuery (4), then the original Transformed XML from the POST method is transformed using the transformed/assembled XSL template (5) before being sent off to an external service (6). 这些模板本身然后使用xQuery(4)进行转换和组装,然后使用转换后的XSL模板(5)转换来自POST方法的原始Transformed XML(5),然后再发送给外部服务(6)。

This all sort of works right now by storing the body of various steps as exchange properties, including the static XSL templates from the database using a processor. 现在,通过将各个步骤的主体存储为交换属性(包括使用处理器从数据库中获取的静态XSL模板)来完成所有这些工作。 The exchange properties are accessible from my xQuery so all is good. 可从我的xQuery访问交换属性,因此一切都很好。 At various steps I also replace the body with values temporarily stored as exchanged properties. 在各个步骤中,我还用临时存储为交换属性的值替换了主体。 This all seems clumsy to me... 这一切对我来说都很笨拙...

My real question is what is the best way to cache all the XSL templates during application startup so that subsequent calls to the DB are unnecessary? 我真正的问题是在应用程序启动期间缓存所有XSL模板的最佳方法是什么,这样就不必对数据库进行后续调用了? They are static and less than 10 in number. 它们是静态的,数量少于10。 I was just thinking of storing them in a classic singleton from a processor in a route that executes at startup, then accessing them using the processor used in step 3 to set exchange variables to the templates I need. 我只是想将它们以经典的单例形式从处理器存储在启动时执行的路由中,然后使用步骤3中使用的处理器访问它们以将交换变量设置为所需的模板。 Is there a better way of doing this? 有更好的方法吗?

I opted to make a Spring bean to hold my XSL Templates and then do this in my RouteBuilder class so it would cache the templates upon startup and make them accessible to other routes: 我选择制作一个Spring bean来保存我的XSL模板,然后在RouteBuilder类中执行此操作,这样它将在启动时缓存模板并使其他路径可以访问它们:

@Autowired
XSLTemplates xslTemplates;

@Override
public void configure() throws Exception {
    // get the templates from the database upon app startup and store
    from("timer://mytimer?repeatCount=1")
        .to("sql:select ID, XSL_TEMPLATE from table").split()
        .simple("${body}")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                Map<String, Object> row = exchange.getIn().getBody(Map.class);
                if (row.get("ID").toString().equals("fooTemplate")) {
                    xslTemplates.setFooTemplate(row.get("XSL_TEMPLATE).toString());
                }  else... everything else

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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