You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Willem Jiang <ni...@iona.com> on 2007/08/01 04:40:24 UTC

Re: Application Hang at Exit with Simple Frontend

Hi Mike,

You can use the Jetty as a servlet container which hosts the CXF servlet.
Here are the codes :

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;

import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

public class JettyServer {

    public static void main(String[] args) throws Exception {
  
        Server httpServer = new Server(9000);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        httpServer.setHandler(contexts);
       
        Context root = new Context(contexts,"/services",Context.SESSIONS);
        CXFServlet servlet = new CXFServlet();
        root.addServlet(new ServletHolder(servlet), "/*");
       
        httpServer.start();
        Bus bus = servlet.getBus();
        // we should use the bus which is loaded by CXFServlet
        BusFactory.setDefaultBus(bus);

        // set the address, you can access the service from this url 
"http://localhost:9000/services/GreeterImple"
        String address = "/" + GreeterImpl.class.getSimpleName();      
        // Here we use the jaxws API to publish the service.      
        Endpoint endpoint = Endpoint.create(new GreeterImpl());
        endpoint.publish(address);

         // Create our service implementation
        HelloImplhelloWorldImpl helloImpl = new HelloImpl();

        // Create the server with simple front end api
        ServerFactoryBean svrFactory = new ServerFactoryBean();
        svrFactory.setBus(bus);
        svrFactory.setServiceClass(Hello.class);
        svrFactory.setAddress("/Hello");
        svrFactory.setServiceBean(helloImpl);
        svrFactory.create();
    }
}


Willem.


Pilone, Mike wrote:
> Thanks for looking into it Willem. I'm not sure if you saw my other
> email in this thread, but it looks like the shutdown order in the
> JettyHttpServletEngine is not currently correct as well. That seems to
> be causing the WARNING log messages when my work-around code is used. I
> don't know if you want to include this as part of the issue report or in
> a new report. BTW, I also noticed that the hang problem does not show up
> in unit tests. My guess is that JUnit is calling System.exit or some
> other hard termination of the JVM.
>
> Is there any better work-around? Is it possible to use Jetty directly
> and simply pass in a the CXFServlet? I'm relatively new to CXF (coming
> from XFire) so I'm not completely familiar with the architecture.
>
> Thanks,
> -mike
>
>
> -----Original Message-----
> From: Willem Jiang [mailto:ning.jiang@iona.com] 
> Sent: Tuesday, July 31, 2007 3:42 AM
> To: cxf-user@incubator.apache.org
> Subject: Re: Application Hang at Exit with Simple Frontend
>
> Hi Mike,
>
> Yes, we don't shutdown the Jetty Engine when the mServer.stop is called.
> If you looking into the code you will find there is a note in the 
> JettyHTTPServerEngine's removeServant(URL url)
>
>             /* Bug in Jetty, we cannot do this.  If we restart later, 
> data goes off
>              * someplace unknown
>             if (servantCount == 0) {
>                 try {
>                    ....
>             }*/
>
> I just added the shutdown engine's code below the comments and tested it
>
> against the systest. There were some  tests (such as ws.rm , mtom
> )failed.
> It looks like if we shutdown the engine, when the engine start again , 
> the engine can not get the request info again , specially the http keep 
> alive message.
> Current I can't reproduce this Jetty bug  by adding the unit test which 
> simply calls start and shutdown method the JettyHTTPServerEngine.
> It may take some time to resolve this issue.
>
> Now I am afraid you still need to shutdown the Jetty Engine by using the
>
> code that you showed below.
> Anyway, I filled a JIRA for it[1], maybe we can find a better way to 
> resolve this later.
>
> [1] https://issues.apache.org/jira/browse/CXF-865
>
> Willem.
>
> Pilone, Mike wrote:
>   
>> Hello all,
>>  
>> I was previously using XFire and I am moving my application over to
>>     
> CXF. Right now I am just prototyping some application features and I
> needed a quick solution for remote administration. The simple
> frontend/embedded Jetty solution was working really well for me with
> XFire, but I am having a problem in CXF. It appears that stopping the
> server in CXF is not shutting down the Jetty engine, which causes my
> application to hang because Jetty's threads are still blocking on the
> server socket accept. After a shutdown, I still see Thread [btpool0-0 -
> Acceptor0 SelectChannelConnector@0.0.0.0:10001] (Running).
>   
>>  
>> I have a simple test application pasted below that can reproduce the
>>     
> problem. At this point I had to add some calls to get the destination
> and cast it to a Jetty specific class to get things to exit. Is there a
> better way to do this? Is there something I'm not configuring correctly?
> Any help is appreciated.
>   
>>  
>> This is under Java 1.5 with CXF 2.0. Jetty is being included from the
>>     
> CXF libs directory.
>   
>>  
>>  
>> package org.mpilone.cxftest;
>>  
>> import java.io.IOException;
>> import org.apache.cxf.endpoint.Server;
>> import org.apache.cxf.frontend.ServerFactoryBean;
>>  
>> public class CxfJettyTest
>> {
>>   public void doIt()
>>   {
>>     System.out.println("Running test method");
>>   }
>>  
>>   public static void main(String[] args) throws IOException
>>   {
>>     // Create an Service and Server
>>     ServerFactoryBean serverFactory = new ServerFactoryBean();
>>     serverFactory.setServiceClass(CxfJettyTest.class);
>>     serverFactory.setServiceBean(new CxfJettyTest());
>>     serverFactory.setAddress("http://localhost:10001/RemoteApi");
>>     Server mServer = serverFactory.create();
>>  
>>     mServer.start();
>>     System.in.read();
>>     mServer.stop();
>>  
>>     // Adding these lines allows the application to exit, but
>>     // WARNING: EXCEPTION
>>     // java.nio.channels.ClosedChannelException
>>     // log statements are produced.
>>  
>> // JettyHTTPDestination jettyDest = (JettyHTTPDestination)
>> // mServer.getDestination();
>> // JettyHTTPServerEngine jettyEngine = (JettyHTTPServerEngine)
>> // jettyDest.getEngine();
>> // jettyEngine.shutdown();
>>  
>>     System.out.println("Exiting");
>>   }
>> }
>>
>> Michael Pilone
>> Senior Software Engineer
>> Vangent, Inc.
>> Blueprint Solutions
>> (c) 703-969-7493
>> Updated email address: michael.pilone@vangent.com
>>
>> Visit our new web site: www.vangent.com
>>
>>
>>     
> ************************************************************************
> **** 
>   
>> This email may contain material confidential to
>> Pearson.  If you were not an intended recipient, 
>> please notify the sender and delete all copies. 
>> We may monitor email to and from our network. 
>>
>>     
> ************************************************************************
> ****
>   
>>   
>>     
> **************************************************************************** 
> This email may contain material confidential to
> Pearson.  If you were not an intended recipient, 
> please notify the sender and delete all copies. 
> We may monitor email to and from our network. 
> ****************************************************************************
>
>