You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Rehtron <ne...@gmail.com> on 2011/07/01 11:07:35 UTC

How to start & stop embedding Tomcat correctly?

Dear all,

I use org.apache.catalina.startup.Tomcat to do unit test,  but the instance
of which cannot be started or stopped correctly.

I start and stop embedding tomcat for each test case, but first test can be
passed, the second one will be failed by 503 Service,  unavailable, the
tomcat.start() method has been invoked, but the tomcat return 503 error.

My code for tomcat start & stop:

public static Tomcat startServer() throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8088);
 Context context = tomcat.addContext("",
System.getProperty("java.io.tmpdir"));
 Wrapper servlet = Tomcat.addServlet(
context,
MyServlet.class.getSimpleName(),
MyServlet.class.getName());
 context.addServletMapping("/*", servlet.getName());
tomcat.start();
return tomcat;
}

public static void stop(Tomcat tomcat) {
if (tomcat == null || tomcat.getServer() == null) {
return;
}
        if (tomcat.getServer().getState() != LifecycleState.DESTROYED) {
            if (tomcat.getServer().getState() != LifecycleState.STOPPED) {
                try {
tomcat.stop();
} catch (LifecycleException e) {
LOGGER.error("Stop tomcat error.", e);
}
            }
            try {
tomcat.destroy();
} catch (LifecycleException e) {
LOGGER.error("Destroy tomcat error.", e);
}
        }
}
}

Thanks.

Zijian

Re: How to start & stop embedding Tomcat correctly?

Posted by Rehtron <ne...@gmail.com>.
OK,  thank you very much for your help!


2011/7/1 Mark Thomas <ma...@apache.org>

> On 01/07/2011 10:56, Rehtron wrote:
> > Windows XP, did you mean I should use different port for every test?
>
> I have seen all OSes hold on to ports after Tomcat has shutdown. Windows
> isn't as bad as Linux but it does happen sometimes.
>
> Yes, you need to use a new port for each test. Again, take a look at
> Tomcat's own unit tests.
>
> Mark
>
> >
> > Thanks.
> >
> > 2011/7/1 Mark Thomas <ma...@apache.org>
> >
> >> On 01/07/2011 10:29, Rehtron wrote:
> >>> Yes, I use same port for all test, but I think the port should be
> release
> >>> after tomcat stopped.
> >>
> >> What OS are you using?
> >>
> >> Mark
> >>
> >>>
> >>> thanks.
> >>>
> >>> 2011/7/1 Mark Thomas <ma...@apache.org>
> >>>
> >>>> On 01/07/2011 10:07, Rehtron wrote:
> >>>>> Dear all,
> >>>>>
> >>>>> I use org.apache.catalina.startup.Tomcat to do unit test,  but the
> >>>> instance
> >>>>> of which cannot be started or stopped correctly.
> >>>>>
> >>>>> I start and stop embedding tomcat for each test case, but first test
> >> can
> >>>> be
> >>>>> passed, the second one will be failed by 503 Service,  unavailable,
> the
> >>>>> tomcat.start() method has been invoked, but the tomcat return 503
> >> error.
> >>>>
> >>>> Take a look at how Tomcat does this in it's own unit tests. Tomcat
> uses
> >>>> a new port for each test to prevent this sort of problem.
> >>>>
> >>>> Mark
> >>>>
> >>>>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >>>> For additional commands, e-mail: users-help@tomcat.apache.org
> >>>>
> >>>>
> >>>
> >>
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >> For additional commands, e-mail: users-help@tomcat.apache.org
> >>
> >>
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to start & stop embedding Tomcat correctly?

Posted by Mark Thomas <ma...@apache.org>.
On 01/07/2011 10:56, Rehtron wrote:
> Windows XP, did you mean I should use different port for every test?

I have seen all OSes hold on to ports after Tomcat has shutdown. Windows
isn't as bad as Linux but it does happen sometimes.

Yes, you need to use a new port for each test. Again, take a look at
Tomcat's own unit tests.

Mark

> 
> Thanks.
> 
> 2011/7/1 Mark Thomas <ma...@apache.org>
> 
>> On 01/07/2011 10:29, Rehtron wrote:
>>> Yes, I use same port for all test, but I think the port should be release
>>> after tomcat stopped.
>>
>> What OS are you using?
>>
>> Mark
>>
>>>
>>> thanks.
>>>
>>> 2011/7/1 Mark Thomas <ma...@apache.org>
>>>
>>>> On 01/07/2011 10:07, Rehtron wrote:
>>>>> Dear all,
>>>>>
>>>>> I use org.apache.catalina.startup.Tomcat to do unit test,  but the
>>>> instance
>>>>> of which cannot be started or stopped correctly.
>>>>>
>>>>> I start and stop embedding tomcat for each test case, but first test
>> can
>>>> be
>>>>> passed, the second one will be failed by 503 Service,  unavailable, the
>>>>> tomcat.start() method has been invoked, but the tomcat return 503
>> error.
>>>>
>>>> Take a look at how Tomcat does this in it's own unit tests. Tomcat uses
>>>> a new port for each test to prevent this sort of problem.
>>>>
>>>> Mark
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>>
>>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to start & stop embedding Tomcat correctly?

Posted by Rehtron <ne...@gmail.com>.
Windows XP, did you mean I should use different port for every test?

Thanks.

2011/7/1 Mark Thomas <ma...@apache.org>

> On 01/07/2011 10:29, Rehtron wrote:
> > Yes, I use same port for all test, but I think the port should be release
> > after tomcat stopped.
>
> What OS are you using?
>
> Mark
>
> >
> > thanks.
> >
> > 2011/7/1 Mark Thomas <ma...@apache.org>
> >
> >> On 01/07/2011 10:07, Rehtron wrote:
> >>> Dear all,
> >>>
> >>> I use org.apache.catalina.startup.Tomcat to do unit test,  but the
> >> instance
> >>> of which cannot be started or stopped correctly.
> >>>
> >>> I start and stop embedding tomcat for each test case, but first test
> can
> >> be
> >>> passed, the second one will be failed by 503 Service,  unavailable, the
> >>> tomcat.start() method has been invoked, but the tomcat return 503
> error.
> >>
> >> Take a look at how Tomcat does this in it's own unit tests. Tomcat uses
> >> a new port for each test to prevent this sort of problem.
> >>
> >> Mark
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >> For additional commands, e-mail: users-help@tomcat.apache.org
> >>
> >>
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to start & stop embedding Tomcat correctly?

Posted by Mark Thomas <ma...@apache.org>.
On 01/07/2011 10:29, Rehtron wrote:
> Yes, I use same port for all test, but I think the port should be release
> after tomcat stopped.

What OS are you using?

Mark

> 
> thanks.
> 
> 2011/7/1 Mark Thomas <ma...@apache.org>
> 
>> On 01/07/2011 10:07, Rehtron wrote:
>>> Dear all,
>>>
>>> I use org.apache.catalina.startup.Tomcat to do unit test,  but the
>> instance
>>> of which cannot be started or stopped correctly.
>>>
>>> I start and stop embedding tomcat for each test case, but first test can
>> be
>>> passed, the second one will be failed by 503 Service,  unavailable, the
>>> tomcat.start() method has been invoked, but the tomcat return 503 error.
>>
>> Take a look at how Tomcat does this in it's own unit tests. Tomcat uses
>> a new port for each test to prevent this sort of problem.
>>
>> Mark
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to start & stop embedding Tomcat correctly?

Posted by Rehtron <ne...@gmail.com>.
Yes, I use same port for all test, but I think the port should be release
after tomcat stopped.

thanks.

2011/7/1 Mark Thomas <ma...@apache.org>

> On 01/07/2011 10:07, Rehtron wrote:
> > Dear all,
> >
> > I use org.apache.catalina.startup.Tomcat to do unit test,  but the
> instance
> > of which cannot be started or stopped correctly.
> >
> > I start and stop embedding tomcat for each test case, but first test can
> be
> > passed, the second one will be failed by 503 Service,  unavailable, the
> > tomcat.start() method has been invoked, but the tomcat return 503 error.
>
> Take a look at how Tomcat does this in it's own unit tests. Tomcat uses
> a new port for each test to prevent this sort of problem.
>
> Mark
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to start & stop embedding Tomcat correctly?

Posted by Mark Thomas <ma...@apache.org>.
On 01/07/2011 10:07, Rehtron wrote:
> Dear all,
> 
> I use org.apache.catalina.startup.Tomcat to do unit test,  but the instance
> of which cannot be started or stopped correctly.
> 
> I start and stop embedding tomcat for each test case, but first test can be
> passed, the second one will be failed by 503 Service,  unavailable, the
> tomcat.start() method has been invoked, but the tomcat return 503 error.

Take a look at how Tomcat does this in it's own unit tests. Tomcat uses
a new port for each test to prevent this sort of problem.

Mark



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org