Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.wildfly.test.suspendresumeendpoint;
  
 

Author(s):
Stuart Douglas
 
 public class TestUndertowService implements Service<TestUndertowService> {
 
 
     private static final AtomicInteger COUNT = new AtomicInteger();
 
 
     public static final ServiceName SERVICE_NAME = ..append("test-undertow-server");
     public static final String SKIP_GRACEFUL = "skip-graceful";
 
     private volatile Undertow undertow;
 
     @Override
     public void start(StartContext contextthrows StartException {        //add graceful shutdown support
         final SuspendResumeHandler suspendResumeHandler = new SuspendResumeHandler();
         final ControlPoint controlPoint = .getValue().getControlPoint("test""test");
 
         final ExchangeCompletionListener exchangeCompletionListener = new ExchangeCompletionListener() {
             @Override
             public void exchangeEvent(HttpServerExchange exchangeNextListener nextListener) {
                 controlPoint.requestComplete();
                 nextListener.proceed();
             }
         };
 
         HttpHandler shutdown = new HttpHandler() {
             @Override
             public void handleRequest(final HttpServerExchange exchangethrows Exception {
                 if(exchange.isInIoThread()) {
                     exchange.dispatch(this);
                     return;
                 }
                 final int count = .incrementAndGet();
                 if(exchange.getQueryParameters().containsKey()) {
                     //bit of a hack, allows to send in some requests even when the server is paused
                     //very useful for testing
                     ..println("Skipping request " + count + " " + exchange);
                     suspendResumeHandler.handleRequest(exchange);
                     return;
                 }
                 ..println("Attempting " + count + " " + exchange);
                 RunResult result = controlPoint.beginRequest();
                 if (result == .) {
                     exchange.dispatch(new Runnable() {
                         @Override
                         public void run() {
                             controlPoint.queueTask(new Runnable() {
                                 @Override
                                 public void run() {
                                     exchange.addExchangeCompleteListener(exchangeCompletionListener);
                                     exchange.dispatch(suspendResumeHandler);
                                 }
                             }, exchange.getIoThread(), 1000, new Runnable() {
                                 @Override
                                 public void run() {
                                     ..println("Rejected " + count + " " + exchange);
                                     exchange.setResponseCode(503);
                                     exchange.endExchange();
                                 }
                             }, true);
                         }
                     });
 
                     return;
                 }
                 exchange.addExchangeCompleteListener(exchangeCompletionListener);
                 suspendResumeHandler.handleRequest(exchange);
             }
         };
          = Undertow.builder().addHttpListener(8080 + .getValue().getPortOffset(), "0.0.0.0").setHandler(shutdown).build();
         .start();
     }
 
     @Override
     public void stop(StopContext context) {
         .stop();
         = null;
    }
    @Override
        return this;
    }
        return ;
    }
        return ;
    }
New to GrepCode? Check out our FAQ X