首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在每个java Restful服务中使用一个apache camel上下文对象

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

Stack Overflow用户
提问于 2013-11-21 18:17:02
回答 1查看 751关注 0票数 0

我需要执行所有操作,比如使用restful服务创建quartz-2调度器和删除一个Apache camel上下文。当我尝试使用下面的代码上下文时,它创建了一个新的.each对象。我不知道如何修复它,也不知道在哪里需要启动apache camel上下文对象。

这是我的代码

这是我的java restful服务,它被调用到quartz调度器。

java Rest服务。

代码语言:javascript
复制
  @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类,它是调度作业。

代码语言:javascript
复制
    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上下文对象上启动作业调度。如果发送停止路由的请求,那么还会创建新的apache上下文对象,因此我无法重置或停止quartz-2调度器。

EN

回答 1

Stack Overflow用户

发布于 2013-11-25 18:10:06

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20118126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档