2013-11-21 15 views
0

我需要執行所有操作,例如創建quartz-2調度程序,並使用restful service僅在一個Apache駱駝上下文 上刪除。當我嘗試使用下面的代碼。每次它創建新的上下文對象。我不知道如何解決它或我需要啓動apache駱駝上下文對象。如何在每個java中使用一個apache camel上下文對象Restful services

這是我的代碼

這是我的Java RESTful服務是調用石英調度。

java Rest Services。

@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駱駝上下文對象的作業調度。如果發送請求停止路由,那麼也創建新的Apache上下文對象,所以我無法重置或停止石英2調度器。

回答

1

爲每個請求創建駱駝上下文不是一個好習慣。 我建議您使用camel-restletcamel-cxfrs將創建和刪除調度程序的請求委託給另一個駱駝上下文。

相關問題