Performance by using one logger object per application in Java - java

There are number of method to create logger instance.
one instance of Logger per class
one instance of Logger per thread
one instance of Logger per application
Can any one suggest performance on each method?
Currently i am using one logger object per application so Is this down multithreaded application performance?.

A good tracking resource is Jamon, I guess you know it. Inside an EE application there is a simple way to "hook" it to every method call, in order to trace all method's execution time. In this way, you could analyze the impact of your "added" log calls
Back to your question, I don't think there should be performance issues, as the log output is anyway serialized and instantiating per method, classs or even application is just a matter of used memory

Related

Should JavaDelegate classes for Camunda BPM be thread safe?

The main question is about static fields and singleton instances (for configs, etc.) - are instances of one process running in different threads, as usual servlet requests?
If look deeper - do different #ProcessApplication run in one JVM and will see the same singletons? I don't think so. I know exactly that their classes don't see each other and can have equal names (because of different classLoaders?)
Haven't found any meaningful info on these important themes about Camunda, will appreciate your answers.
I had this same question for one of our scenario while back, and read their Javadoc as mentioned here for a servlet container. Extracting Javadoc,
Invocation Semantics
When the {#link #execute(java.util.concurrent.Callable)} method is
invoked, the servlet process application modifies the context
classloader of the current Thread to the classloader that loaded the
application-provided subclass of this class. This allows,
the process engine to resolve {#link JavaDelegate} implementations using the classloader of the process application
This pretty much explain everything you want to know, since the behavior is very similar to how a web container operates. If you want to know how other container implementations behaves, you can check the respective Javadocs of classes in this package.
To answer your question:
Yes. Thread-safety is required for the shared-resources accessed by JavaDelegate in the same process application. According to the documentation (see below) they create a new instance of delegate each time a task is going to be executed.
Note!
Each time a delegation class referencing activity is executed, a
separate instance of this class will be created. This means that each
time an activity is executed there will be used another instance of
the class to call execute(DelegateExecution).
Therefore, at any time there can be many delegate instances actively running due to the multiple invocation of Process Definitions. So, if they are accessing a shared resource, then they need to be synchronized (thread-safe), because, that shared resources (static or singleton) is local to the process application and loaded by respective application classloader according to above Invocation Semantics Javadoc.
Hope this helps.

How to release a Log4J logger

I'm creating a program, that will run 24/7 and will continually process multiple tasks. These tasks can run in parallel. Every task will have it's independent log file. So for every task I want to create own logger like this:
Logger logger = Logger.getLogger("taskID");
How can I correctly release the logger so it is no longer in memory, after the task is done?
There is no way to "release" a Logger object. But that's OK. If it is reachable you can still use it ... and it shouldn't be "released". If it is unreachable, the GC will reclaim it.
By the way, if you are really talking about log4j, then the method you call to get hold of a named logger is Logger.getLogger(String). It is defined to return an existing instance (with the same name) if one exists, so you don't need to worry about creating lots of copies of the same logger.
This is not the way a Logger should be instantiated. You must always make them static and final. Doing so you don't have to worry about it anymore as it's going to be only and only one instance of Logger per class.
Take a look at the official documentation and some manuals online. This book is also very good to get started.
PS: On the other hand, I would recomend you the use of SLF4j as façade.

using logback in a client server program

I need to use logback in a client server program, for each request that comes to server, it creates a new service which will run in a separate thread. I need to log actions that happen during service excecution. But i dont want to generate separate logger object for each service thread. I know that one solution would be to set the logger object as static. So it wont be instanciated every time but is there any standard solution for this kind of problem. bellow are some code snippets from my source code:
The server class which creates a separate servcie thread for each request:
1: a logger specific for server class.
2: for each incomming request that comes to server we generate a new thread (new instance of service class),but the question is that we dont want to have a logger instances for each service instance (i guess it is a bad practice!)
and here is the service class :
*:logger is defined static so it wont be instanciated for each service class instance:
i know that one solution would be to set the logger object as static so it wont be instanciated every time but is there any standard solution for this kind of problem.
This is what I do in my application. It works great.
Many of my classes have this as the first line:
public class SomeClass {
private static final Logger LOG = LoggerFactory.getLogger(SomeClass.class);
// the rest of the class
}
Also, if you want the log messages to reflect which overall request is the one doing the logging, you should use MDC:
One of the design goals of logback is to audit and debug complex distributed applications. Most real-world distributed systems need to deal with multiple clients simultaneously. In a typical multithreaded implementation of such a system, different threads will handle different clients. A possible but slightly discouraged approach to differentiate the logging output of one client from another consists of instantiating a new and separate logger for each client. This technique promotes the proliferation of loggers and may increase their management overhead.
Read the entire link, it does a better job of explaining MDC than I ever could.

Log4J - One log file per thread in an environment with dynamic thread creation

Let me begin with a brief explanation of the system I am using:
We have a system that runs as a Daemon, running work-units as they come in. This daemon dynamically creates a new Thread when it is given a new work-unit. For each of these units of work, I need Log4J to create a new log file to append to - the filename will be provided at runtime, when the new log file must be created. This daemon must be able to remain alive indefinitely, which I believe causes some memory concerns, as I will explain.
My first thought was to create a new Logger for each work-unit, naming it after the thread, of course. The work-unit's thread retains a reference to that Logger. When the unit is finished, it will be garbage-collected, but the problem is that Log4J itself retains a reference to the Logger, which will never be used again. It seems likely that all these Loggers will cause the VM to run out of memory.
Another solution: subclass Filter, to filter Appenders by the thread names, and place them on the same Logger. Then, remove the Appenders as the work-units complete. Of course, this necessitates adding the code to remove the appenders. That will be a lot of code changes.
I've looked into NDC and MDC, which appear to be intended for managing interleaved output to the same file. I've considered proposing this as a solution, but I don't think it will be accepted.
I want to say that Log4J appears that it is intended not to function this way, that is, dynamically creating new log files at runtime, as they are required (or desired). So I am not sure which direction to look in next - Is log4j not the solution here, or did I comPLETELY miss something? Have I not looked closely enough at NDC? Or is my concern with Log4J holding onto Loggers a nonissue for reasons I don't see?
You could just create a new log method that wraps the normal log method, and append thread id.
Something like the following (oversimplified, but you get the idea). Log4j is already thread save I believe, so as long as you aren't logging a ton, you should be fine. Then you can easily grep on thread id.
public log(long id, String message)
{
logger.log("ThreadId: id + "message: " + message);
}
ThreadLocal appears to be the solution for you.

Java update program configuration

I have a multithreaded program that loads its configuration on startup. The configuration is then handed down to the threads via their constructors.
But now I want to load one new config instance regularly, and pass it to the threads.
One idea would be to make the reference in the thread class to the config file volatile. Then, when an updated config instance is available, call a update update(Config c) method.
Is this the way to go? I will have terrible performance because every time the thread needs some setting it has to do all that volatile checking stuff.
Some better suggestions? Best practice? Maybe don't make it volatile and hope that the processor fetches that new object from main memory from time to time?
You could encapsulate all of your configuration values in a single immutable object, and when the configuration changes create a new instance of the object, and pass it to the threads, through listeners or explicit calling. The object has no volatile fields, only the object itself could be written in a volatile variable (or an AtomicReference).
The direct volatile approach with no other synchronization mechanisms is dangerous: you could read a halfway-rewritten configuration.
In any case, the performance impact on your application is likely to be negligible. Think about optimization later, if you find this is really a bottleneck.
Actually, are you sure that will have terrible performance?
If volatile used mostly for reading it's performance is not that bad. I'd recommend to try volatile first and measure performance degradation and only if it's significant then do any rework.
If you are really worrying about rapid volatile reads - then in you run method in thread you could have check for timeout - if 60 seconds passed since last config read - then reread it. Logic will reversed from update(Config c), to
if(moreThan60SecondsPassed)
{
localConfig = configconfigHolder.getConfig();
}
Also, if you'll be using non volatile - you won't get half read config. The danger is that you could have some threads not see updated value forever (no happens-before relationship).
Bw, did you consider recreating threads on config update? In this case you still could pass config through constructor. It depends on how often you want to update configuration.
You can use observer pattern to notify the threads of new config via listeners.
There is no way you can avoid volatile checking stuff. Maybe is expensive(do some performance tests) but your program will run correctly.
You may want to look at Commons Configuration:
A common issue with file-based configurations is to handle the reloading of the data file when it changes. This is especially important if you have long running applications and do not want to restart them when a configuration file was updated. Commons Configuration has the concept of so called reloading strategies that can be associated with a file-based configuration. Such a strategy monitors a configuration file and is able to detect changes. A default reloading strategy is FileChangedReloadingStrategy. It can be set on a file-based configuration as follows.
ManagedReloadingStrategy is an alternative to automatic reloading. It allows to hot-reload properties on a running application but only when requested by admin. The refresh() method will force a reload of the configuration source.

Categories