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

标签 java apache-camel restful-architecture

我需要执行所有操作,例如创建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/

相关文章:

java - JVM:是否可以操作帧堆栈?

java - 程序未编译

java - 将此 Camel 路由片段从 Java DSL 转换为 Blueprint xml

rest - 对“REST”一词及其含义的误解由什么构成

rest - 在 REST 中起草一篇文章

mysql - API性能测试连接超时

java - Spring-MVC中如何实现用户锁定?

java - Apache Camel : set cookies for http4 client

java - 如何锁定写入文件以便其他消费者无法使用

java - DatagramSocket 随机停止接收 DatagramPackets