Showing posts with label j2ee. Show all posts
Showing posts with label j2ee. Show all posts

The Executor (thread pool)

Executor (thread pool)

The Executor represents a thread pool that can be shared between components in Tomcat. Historically there has been a thread pool per connector created but this allows you to share a thread pool, between (primarly) connector but also other components when those get configured to support executors
The executor has to implement the
org.apache.catalina.Executor
The executor is a nested element to the Service element. And in order for it to be picked up by the connectors, the Executor element has to appear prior to the Connector element in server.xml
All implementations of Executor support the following attributes:

Attributes

Common Attributes

All implementations of Executor support the following attributes:

Attribute Description

className : The class of the implementation. The implementation has to implement theorg.apache.catalina.Executor interface. This interface ensures that the object can be referenced through its name attribute and that implements Lifecycle, so that it can be started and stopped with the container. The default value for the className is org.apache.catalina.core.StandardThreadExecutor
name : The name used to reference this pool in other places in server.xml. The name is required and must be unique.

Standard Implementation

The default implementation supports the following attributes:

Attribute Description

threadPriority (int) : The thread priority for threads in the executor, the default is Thread.NORM_PRIORITY
daemon (boolean) : Whether the threads should be daemon threads or not, the default is true
namePrefix (String) : The name prefix for each thread created by the executor. The thread name for an individual thread will be namePrefix+threadNumber
maxThreads (int) : The max number of active threads in this pool, default is 200
minSpareThreads (int) : The minimum number of threads always kept alive, default is 25
maxIdleTime (int) : The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)

Example :

server.xml looks like the following:
<!–The connectors can use a shared executor, you can define one or more named thread pools–>

<Executor name=”tomcatThreadPool” namePrefix=”catalina-exec-” maxThreads=”200″ minSpareThreads=”4″/>

<Connector executor=”tomcatThreadPool” port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”10000″ maxKeepAliveRequests=”1″ redirectPort=”8443″ />

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
However, in the Tomcat manager (http://localhost/manager/status) it shows to following

http-8080: Max threads: -1 Current thread count: -1 Current thread busy:-1
jk-8009: Max threads: 200 Current thread count: 4 Current thread busy: 1

0 threads will be created after the tomcat startup, depending on the number of concurrent requests number of threads will be created in the thread pool, for instance if there are 2 concurrent requests then 2 threads will be created in the pool and these threads are further reused by subsequent requests until more concurrent requests come where further threads will be created. Because of the minSpareThreads value of 4 all the threads created up to 4 will not be discarded irrespective of whether the threads are idle or not.

As shown in standard implementation table Thread pools shrink based on the maxIdleTime which by default is 60000 ms/ 60 secs / 1 minute meaning if a thread is idle for 60 secs tomcat will discard it from the pool.

Java Override annotation

Using the @Override annotation acts as a compile-time safeguard against a common programming mistake. It willthrow a compilation error if you have the annotation on a method you’re not actually overriding the superclass method.
The most common case where this is useful is when you are changing a method in the base class to have a different parameter list. A method in a subclass that used to override the superclass method will no longer do so due the changed method signature. This can sometimes cause strange and unexpected behavior, especially when dealing with complex inheritance structures. The @Override annotation safeguards against this.
To take advantage from compiler checking you should always use Override annotation. But don’t forget that Java Compiler 1.5 will not allow this annotation when overriding interface methods. You just can use it to override class methods (abstract, or not).
Some IDEs, as Eclipse, even configured with Java 1.6 runtime or higher, they maintain compliance with Java 1.5. To avoid that behaviour you must go to: Project Properties ->Java Compiler -> Check “Enable Project Specific Settings” -> Choose “Compiler Compliance Level” = 6.0, or higher
Java’s @Override annotation follows when
  • • Used only on method declarations.
  • • Indicates that the annotated method declaration overrides a declaration in supertype.
If used consistently, it protects you from a large class of nefarious bugs.
Use @Override annotation to avoid these bugs: (Spot the bug in the following code:)
public class Bigram {
private final char first;
private final char second;
public Bigram(char first, char second) {
this.first = first;
this.second = second;
}





public boolean equals(Bigram b) {
return b.first == first && b.second == second;
}
public int hashCode() {
return 31 * first + second;
}
public static void main(String[] args) {
Set<Bigram> s = new HashSet<Bigram>();
for (int i = 0; i < 10; i++)
for (char ch = ‘a’; ch <= ‘z’; ch++)
s.add(new Bigram(ch, ch));
System.out.println(s.size());
}
}
The main program repeatedly adds twenty-six bigrams, each consisting of two identical lowercase letters, to a set. Then it prints the size of the set. You might expect the program to print 26, as sets cannot contain duplicates. If you try running the program, you’ll find that it prints not 26 but 260. What is wrong with it?
Clearly, the author of the Bigram class intended to override the equals method (Item-8) and even remembered to override hashCode in tandem (Item-9 )
Unfortunately, our hapless programmer failed to override equals, overloading it instead (Item-41) To override Object.equals, you must define an equals method whose parameter is of type Object, but the parameter of Bigram’s equals method is not of type Object, so Bigram inherits the equals method from
Object. This equals method tests for object identity, just like the == operator.
Each of the ten copies of each bigram is distinct from the other nine, so they are deemed unequal by Object.equals, which explains why the program prints 260.
Luckily, the compiler can help you find this error, but only if you help the compiler by telling it that you intend to override Object.equals. To do this,
annotate Bigram.equals with @Override, as shown below:
@Override public boolean equals(Bigram b) {
return b.first == first && b.second == second;
}
If you insert this annotation and try to recompile the program, the compiler will generate an error message like this:
Bigram.java:10: method does not override or implement a method from a supertype
@Override public boolean equals(Bigram b) {
^
You will immediately realize what you did wrong, slap yourself on the forehead, and replace the broken equals implementation with a correct one (Item 8):
@Override public boolean equals(Object o) {
if (!(o instanceof Bigram))
return false;
Bigram b = (Bigram) o;
return b.first == first && b.second == second;
}
NOTE : Item 8 , Item 9, Item 41 mentioned are part of Effective Java by Joshua Bloch
Therefore, you should use the Override annotation on every method declaration that you believe to override a superclass declaration.
Using the @Overri
de annotation acts as a compile-time safeguard against a common programming mistake. It will throw a compilation error if you have the annotation on a method you’re not actually overriding the superclass method.

Apache MAVEN


MAVEN

Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects each with their own Ant build files that were all slightly different and JARs were checked into CVS. We wanted a standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects.
The result is a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.
Maven is distributed in several formats for your convenience. Use a source archive if you intend to build Maven yourself.

Maven's Objectives

Maven's primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:
  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features
While using Maven doesn't eliminate the need to know about the underlying mechanisms, Maven does provide a lot of shielding from the details.
Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system. Once you familiarize yourself with how one Maven project builds you automatically know how all Maven projects build saving you immense amounts of time when trying to navigate many projects.
Maven provides plenty of useful project information that is in part taken from your POM and in part generated from your project's sources. For example, Maven can provide:
  • Change log document created directly from source control
  • Cross referenced sources
  • Mailing lists
  • Dependency list
  • Unit test reports including coverage
As Maven improves the information set provided will improve, all of which will be transparent to users of Maven.
Maven aims to gather current principles for best practices development, and make it easy to guide a project in that direction.
For example, specification, execution, and reporting of unit tests are part of the normal build cycle using Maven. Current unit testing best practices were used as guidelines:
  • Keeping your test source code in a separate, but parallel source tree
  • Using test case naming conventions to locate and execute tests
  • Have test cases setup their environment and don't rely on customizing the build for test preparation.
  • Maven also aims to assist in project workflow such as release management and issue tracking.
Maven also suggests some guidelines on how to layout your project's directory structure so that once you learn the layout you can easily navigate any other project that uses Maven and the same defaults.

What is Maven Not?

You may have heard some of the following things about Maven:
  • Maven is a site and documentation tool
  • Maven extends Ant to let you download dependencies
  • Maven is a set of reusable Ant scriptlets
Maven does encourage best practices, but we realise that some projects may not fit with these ideals for historical reasons. While Maven is designed to be flexible, to an extent, in these situations and to the needs of different projects, it can not cater to every situation without making compromises to the integrity of its objectives.


More on Javamazon