gpt4 book ai didi

java - 如何在每个java Restful服务中使用一个apache Camel上下文对象

转载 作者:行者123 更新时间:2023-12-01 13:48:59 25 4
gpt4 key购买 nike

我需要执行所有操作,例如创建quartz-2调度程序并仅在一个Apache Camel上下文上删除使用安心的服务。当我尝试使用以下代码时。每次它都会创建新的上下文对象。我不知道如何修复它或者我需要在哪里启动 apache Camel 上下文对象。

这是我的代码

这是我的java Restful 服务,它调用quartz 调度程序。

java 休息服务。

  @Path("/remainder") 
public class RemainderResource {
private static org.apache.log4j.Logger log = Logger.getLogger(RemainderResource.class);
RemainderScheduler remainderScheduler=new RemainderScheduler();
CamelContext context = new DefaultCamelContext();
@POST
@Path("/beforeday/{day}")
public void create(@PathParam("day") int day,final String userdata)
{
log.debug("the starting process of the creating the Remainder");
JSONObject data=(JSONObject) JSONSerializer.toJSON(userdata);
String cronExp=data.getString("cronExp");
remainderScheduler.create(cronExp,day,context);

}
}

这是我的 java 类,它是调度作业。

    public class RemainderScheduler { 


private static org.apache.log4j.Logger log = Logger.getLogger(RemainderScheduler.class);

public void sendRemainder(int day)
{
log.debug("the starting of the sending the Remainder to user");

}

public RouteBuilder createMyRoutes(final String cronExp,final int day)
{
return new RouteBuilder()
{
@Override
public void configure() throws Exception {
log.debug("Before set schedulling");
from("quartz2://RemainderGroup/Remainder? cron="+cronExp+"&deleteJob=true&job.name='RemainderServices'").bean(new RemainderScheduler(), "sendRemainder('"+day+"')").routeId("Remainder")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {

}
})
;
log.debug("after set schedulling");

}
};
}

public void stopService(CamelContext context)
{
log.debug("this is going to be stop the route");
try {
context.stopRoute("Remainder");
context.removeRoute("Remainder");
} catch (Exception e) {
e.printStackTrace();
}

}
public void create(final String cronExp,final int day,CamelContext context)
{
try
{
//this for if all ready exist then stop it.
if(context.getRoute("Remainder")!=null)
stopService(context);

log.debug("the starting of the process for creating the Remaider Services");
context.addRoutes(createMyRoutes(cronExp, day));
context.start();
log.debug("the status for removing the services is"+context.removeRoute("Remainder"));
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
}

如果我执行上面的代码,那么每个 java Restful 请求都会创建新的上下文对象。它将在新的 apache Camel 上下文对象上启 Action 业调度。如果发送停止路由的请求,那么它也会创建新的 apache 上下文对象,因此我无法重置或停止quartz-2 调度程序。

最佳答案

为每个请求创建一个 Camel 上下文并不是一个好的做法。我建议您使用camel-restletcamel-cxfrs将创建和删除调度程序的请求委托(delegate)给另一个 Camel 上下文。

关于java - 如何在每个java Restful服务中使用一个apache Camel上下文对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118126/

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