当前位置: 首页 > 知识库问答 >
问题:

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

奚才良
2023-03-14

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

这是我的密码

  @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); 

    } 
} 
    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(); 
           } 
      } 
 } 

共有1个答案

罗渝
2023-03-14

为每个请求创建camel上下文不是一个好的做法。我建议您使用camel-restlet或camel-cxfrs将create and delete调度器的请求委托给另一个camel上下文。

 类似资料: