【问题标题】:how to use one apache camel context object in each java Restful services如何在每个 java Restful 服务中使用一个 apache camel 上下文对象
【发布时间】:2013-12-05 18:07:30
【问题描述】:

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

这是我的代码

这是我调用石英调度程序的 java restful 服务。

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 类,它是 schedule job 。

    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 调度程序。

【问题讨论】:

    标签: java apache-camel restful-architecture


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多