[
https://issues.apache.org/jira/browse/LOG4J2-1236?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15091291#comment-15091291
]
Steven Swor edited comment on LOG4J2-1236 at 1/10/16 11:40 PM:
---------------------------------------------------------------
Hi [[email protected]],
I don't think it's an issue so much with invoking {{Thread.join()}} as it is an
issue with calling the whole thing via a shutdown hook. Per Oracle docs'
[Design of the Shutdown Hooks
API|https://docs.oracle.com/javase/8/docs/technotes/guides/lang/hook-design.html],
the shutdown hooks are invoked concurrently, so they don't execute in any
particular order. It's likely that a log4j shutdown hook is being invoked
before yours finishes.
I'm guessing if you can invoke your shutdown procedure
{code:java}
try {
logger.info("transefer server is stopping");
controller.stop();
} catch (Throwable e) {
logger.warn("something goes wrong when stopping transfer Server:\n{}",
Throwables.getStackTraceAsString(e));
} finally {
logger.info("transfer server is down.");
}
{code}
outside of a shutdown hook (e.g. in response to some user input or command) it
will work just fine.
It should be possible to control the ordering a bit more if you provide a
custom
[ShutdownCallbackRegistry|http://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/util/ShutdownCallbackRegistry.html]
but I've not seen a working example of how to do it (naiive approach: write
one to wrap an instance of {{DefaultShutdownCallbackRegistry}} and call your
shutdown code first).
Also, there's a pretty long thread of comments on LOG4J2-658 which may be of
some use to you. I'm not really sure I understand them all, but it might help.
was (Author: sworisbreathing):
Hi [[email protected]],
I don't think it's an issue so much with invoking {{thread.join}} as it is an
issue with calling the whole thing via a shutdown hook. Per Oracle docs'
[Design of the Shutdown Hooks
API|https://docs.oracle.com/javase/8/docs/technotes/guides/lang/hook-design.html],
the shutdown hooks are invoked concurrently, so they don't execute in any
particular order. It's likely that a log4j shutdown hook is being invoked
before yours finishes.
I'm guessing if you can invoke your shutdown procedure
{code:java}
try {
logger.info("transefer server is stopping");
controller.stop();
} catch (Throwable e) {
logger.warn("something goes wrong when stopping transfer Server:\n{}",
Throwables.getStackTraceAsString(e));
} finally {
logger.info("transfer server is down.");
}
{code}
outside of a shutdown hook (e.g. in response to some user input or command) it
will work just fine.
It should be possible to control the ordering a bit more if you provide a
custom
[ShutdownCallbackRegistry|http://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/util/ShutdownCallbackRegistry.html]
but I've not seen a working example of how to do it (naiive approach: write
one to wrap an instance of {{DefaultShutdownCallbackRegistry}} and call your
shutdown code first).
Also, there's a pretty long thread of comments on LOG4J2-658 which may be of
some use to you. I'm not really sure I understand them all, but it might help.
> Logger is set to OFF after thread.join()
> ----------------------------------------
>
> Key: LOG4J2-1236
> URL: https://issues.apache.org/jira/browse/LOG4J2-1236
> Project: Log4j 2
> Issue Type: Question
> Components: API
> Affects Versions: 2.5
> Reporter: Mike Zhang Zhi
> Priority: Minor
>
> I have a launcher class with shutdownHook thread in main method:
> Runtime.getRuntime().addShutdownHook(new Thread() {
> public void run() {
> try {
> logger.info("transefer server is stopping");
> controller.stop();
> } catch (Throwable e) {
> logger.warn("something goes wrong when stopping
> transfer Server:\n{}",
> Throwables.getStackTraceAsString(e));
> } finally {
> logger.info("transfer server is down.");
> }
> }
> });
> In controller to stop the server, I did things below:
> public void stop() {
> if (transferServer.isStart()) {
> transferServer.stop();
> }
> logger.info("Server is down");
> }
> and in server, I stop instances like this:
> public void stop() {
> if (isStart()) {
> running = false;
> for (Map.Entry<String, TransferInstanceInterface> entry :
> transferInstances.entrySet()) {
> TransferInstanceInterface instance = entry.getValue();
> if (instance.isStart()) {
> instance.stop();
> }
> }
> logger.info("Transfer server stopped");
> }
> }
> and in instance I stop two thread:
> public void stop() {
> logger.info("Transfer instance[" + name + "] is stopping...");
> if (messageListener.isStart()) {
> messageListener.stop();
> }
> if (messageProcessor.isStart()) {
> messageProcessor.stop();
> }
> running = false;
> logger.info("Transfer instance[" + name + "] is down.");
> }
> for messageListener, I stopped is like this:
> public void stop() {
> logger.info("Message listener for topic[" + topic + "] is
> stopping...");
> try {
> if (running) {
> running = false;
> if (null != consumer) {
> consumer.close();
> }
> thread.join();
> logger.info("Message listener for topic[" + topic + "] is
> down.");
> }
> } catch (InterruptedException e) {
> logger.error(e.getMessage());
> }
> }
> seems that after thread.join(), all the loggers are set to level off, and
> won't produce any log.
> What the right way to make logger produce log entries after thread.join()?
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]