Jboss Timeout during deployment
Add the below entries in Standalone xml file
Standalone.xml

<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.0">
    <deployment-scanner scan-interval="5000" relative-to="jboss.server.base.dir" path="deployments" deployment-timeout="1000" />
</subsystem>

deployment-timeout=”1000″ is set to 1000 seconds.

Suppressing Errors in JBOSS using ID

JBAS011006: Not installing optional component org.springframework.web.context.request.async.StandardServletAsyncWebRequest due to exception: org.jboss.as.server.deployment.DeploymentUnitProcessingException

Standalone.xml

<subsystem xmlns="urn:jboss:domain:logging:1.1">
      <console-handler name="CONSOLE">
          <filter>
              <not>
                  <match pattern="JBAS011054|JBAS011006"/>
              </not>
          </filter>
      </console-handler>
  </subsystem>     

JBOSS clearing Temp Files
Parameters to be set when server VM start

-Djboss.vfs.cache=org.jboss.virtual.plugins.cache.IterableTimedVFSCache 
-Djboss.vfs.cache.TimedPolicyCaching.lifetime=1440

JBOSS Config parameters

jboss.home.dir - The base directory of the jboss distribution - default: $JBOSS_HOME
jboss.home.url - The base url of the jboss distribution - default $JBOSS_HOME
jboss.lib.url - The url where the kernel jars exist - default: $jboss.home.url/lib
jboss.patch.url - A directory where patch jars exist - default: none
jboss.server.name - The configuration name of the server - default: default
jboss.server.base.dir - The directory where server configurations exist - default: $jboss.home.dir/server
jboss.server.base.url - The url where server configurations exist - default: $jboss.home.url/server
jboss.server.home.dir - The directory for the current configuration - default: $jboss.server.base.dir/$jboss.server.name
jboss.server.home.url - The url for the current configuration - default: $jboss.server.base.url/$jboss.server.name
jboss.server.temp.dir - The directory for temporary files - default: $jboss.server.home.dir/tmp
jboss.server.data.dir - The directory for data files - default: $jboss.server.home.dir/data
jboss.server.config.url - The url for configuration files - default: $jboss.server.home.url/conf
jboss.server.lib.url - The url for static jar files - default: $jboss.server.home.url/lib
jboss.server.log.dir - The directory where the server logs are written - default: $jboss.server.home.dir/log

For Example, if you want JBoss to write the server log file to “C:/Logs/”, then what you should do is to set a system property as this

 -Djboss.server.log.dir=C:/Logs/

java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)
Solution
The Reason for the above error is version mis-match between the various SLF4J API. (e.g. 1.6.x is not backwards compatible with 1.5.x). Make sure you have no duplicate jars in your classpath. If exists exclude them and add these dependencies to your classpath

pom.xml

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.2</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.2</version>
</dependency>

In the above code the Version of API and Implementation should be same(i.e. 1.6.2)

SLF4J is basically an abstraction layer. It is not a logging implementation. It means that if you’re writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J e.g. log4j or the Java logging API. It helps prevent projects from being dependent on lots of logging APIs just because they use libraries that are dependent on them.

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class
Sol

  1. @Entity Should be added in the Model class
  2. If you have missed to add the Model class in the xml file
  3. Make sure the annotation is javax.persistence.Entity, and not org.hibernate.annotations.Entity. The former makes the entity detectable.

————————————————————————————————————————————————————-
javax.naming.NoInitialContextException: Need to specify class name in environment or system property
Sol
“I want to find the telephone number for John Smith, but I have no phonebook to look in”.This exception is thrown when no initial context implementation can be created.JNDI (javax.naming) is all about looking up objects or resources from some directory or provider. To look something up, you need somewhere to look (this is the InitialContext).

————————————————————————————————————————————————————-
hibernate exception: org.hibernate.AnnotationException: No identifier specified for entity
Sol

  1. You are missing a field annotated with @Id. Each @Entity needs an @Id – this is the primary key in the database.
  2. If you don’t want your entity to be persisted in a separate table, but rather be a part of other entities, you can use @Embeddable instead of @Entity.
  3. If you want simply a data transfer object to hold some data from the hibernate entity, use no annotations on it whatsoever – leave it a simple pojo.

————————————————————————————————————————————————————-
org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped
Sol
In the HQL , you should use the java class name and property name of the mapped @Entity instead of the actual table name and column name

For example if your bean class name is UserDetails then the Hibernate code should be as below.Not Tbl_UserDetails instead of UserDetails

 Query query = entityManager. createQuery("Select UserName from UserDetails"); 

The problem can also be because of wrong import

import javax.persistence.Entity;

instead of

import org.hibernate.annotations.Entity;

————————————————————————————————————————————————————-

Failure to find org.jfrog.maven.annomojo:maven-plugin-anno:jar:1.4.0 in http://myrepo:80/artifactory/repo was cached in the local repository, resolution will not be reattempted until the update interval of MyRepo has elapsed or updates are forced -> [Help 1]

The above statement tells that the artifact is cached in local repository.Now the artifact is not going to get downloaded unless it is

  1. It is forced to update from client Side
  2. Forcing from server side the expiration time

From Client Side 3 Solutions

  1. Using Maven Update to force update Snapshots(Mostly doesn’t work)
  2. Deleting the failed directory of Snapshot and forcing it to download
  3. By setting the Time interval for looking for Snapshot
    c:\Users\mugilvannan\maven\conf\settings.xml

    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
          <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
          <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    
  1. AnnotationSessionFactoryBean is used to create session factory if hibernate pojo are annotated
  2. AnnotationSessionFactoryBean is a factory that produces SessionFactory automatically.This is used when you create a sessionFactory object of Hibernate from Spring
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.
    annotation.AnnotationSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="annotatedClasses">
         <list>
           <value>test.package.Foo</value>
           <value>test.package.Bar</value>
         </list>
       </property>
     </bean>
    
  3. This session factory is assigned to all dao beans and hibernate template to do database transaction.
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.
    HibernateTemplate">
    	<property name="sessionFactory">
    	  <ref bean="sessionFactory" />
    	</property>
    </bean>
    <bean id="pageDao" class="com.concretepage.dao.PageDaoImpl">
        <property name="hibernateTemplate">
    	  <ref bean="hibernateTemplate" />
    	</property>
    </bean>
    

Lets take the below scenario where there are Different segment of customers whose discount rates is going to vary based on the segment type and the Store they are visiting

Now the Customer may hold Platinum,Premium or Club Card which avails them some discount on the total bill amount.
Again this discount vary based on the store from which the purchase is made Regular, Franchise, Licensed Store

Visitor.java

package com.mugil.visitor;

public interface Visitor {
	public double getDiscount(FranchiseStore objFranchiseStore);
	public double getDiscount(RegularStore objRegularStore);
	public double getDiscount(LicensedStore objLicensedStore);
}

Visitable.java

package com.mugil.visitor;

public interface Visitable {
	public double acceptDiscount(Visitor objVisitior);	
}

FranchiseStore.java

package com.mugil.visitor;

public class FranchiseStore implements Visitable{
	private int totalBillAmt;

	FranchiseStore(int item) {
		totalBillAmt = item;
	}

	public int getTotalBillAmt() {
		return totalBillAmt;
	}

	@Override
	public double acceptDiscount(Visitor objVisitior) {
		// TODO Auto-generated method stub
		return 0;
	}		
}

RegularStore.java

package com.mugil.visitor;

public class RegularStore implements Visitable {

	private int totalBillAmt;

	RegularStore(int item) {
		totalBillAmt = item;
	}

	public double acceptDiscount(Visitor visitor) {
		return visitor.getDiscount(this);
	}

	public double getTotalBillAmt() {
		return totalBillAmt;
	}		
}

LicensedStore.java

package com.mugil.visitor;

public class LicensedStore implements Visitable {
	
	private int totalBillAmt;

	LicensedStore(int item) {
		totalBillAmt = item;
	}

	public double acceptDiscount(Visitor visitor) {
		return visitor.getDiscount(this);
	}

	public double getTotalBillAmt() {
		return totalBillAmt;
	}

}

ClubCardVisitor.java

package com.mugil.visitor;

public class ClubCardVisitor implements Visitor 
{	
	@Override
	public double getDiscount(FranchiseStore objFranchiseStore) {
		// TODO Auto-generated method stub
		return 0.02;	
	}

	@Override
	public double getDiscount(RegularStore objRegularStore) {
		// TODO Auto-generated method stub
		return 0.03;
	}

	@Override
	public double getDiscount(LicensedStore objLicensedStore) {
		// TODO Auto-generated method stub
		return 0.00;
	}
}

PlatinumCardVisitor.java

package com.mugil.visitor;

public class PlatinumCardVisitor implements Visitor 
{
	@Override
	public double getDiscount(FranchiseStore objFranchiseStore) {
		// TODO Auto-generated method stub
		return 0.05;
	}

	@Override
	public double getDiscount(RegularStore objRegularStore) {
		// TODO Auto-generated method stub
		return 0.07;
	}

	@Override
	public double getDiscount(LicensedStore objLicensedStore) {
		// TODO Auto-generated method stub
		return 0.03;
	}

}

PremiumCardVisitor.java

package com.mugil.visitor;

public class PremiumCardVisitor implements Visitor {
	@Override
	public double getDiscount(FranchiseStore objFranchiseStore) {
		// TODO Auto-generated method stub
		return 0.03;
	}

	@Override
	public double getDiscount(RegularStore objRegularStore) {
		// TODO Auto-generated method stub
		return 0.05;
	}

	@Override
	public double getDiscount(LicensedStore objLicensedStore) {
		// TODO Auto-generated method stub
		return 0.02;
	}
}

GetVisitorDiscount.java

package com.mugil.visitor;

public class GetVisitorDiscount 
{
	public static void main(String[] args) 
	{		
		int totalBillAmount = 500;
		
		System.out.println("Discount for Club Card Visitor @ Franchise Store");
		
		ClubCardVisitor objClubCardVisitor = new ClubCardVisitor();
		FranchiseStore objFranchiseStore = new FranchiseStore(totalBillAmount);
		
		
		double discountPct = objClubCardVisitor.getDiscount(objFranchiseStore);
		double discountAmt = objClubCardVisitor.getDiscount(objFranchiseStore)*totalBillAmount;
		double AmtAfterDis = totalBillAmount - discountAmt; 
		
		
		System.out.println("---------------------------------------------------");
		System.out.println("Total Amount of Purchase " + totalBillAmount);
		System.out.println("Discount Percentage " + discountPct*100 + "%");
		System.out.println("Discount Amount " + discountAmt);
		System.out.println("Total Amount after Discount " + AmtAfterDis);
	}
}

Output

Discount for Club Card Visitor @ Franchise Store
---------------------------------------------------
Total Amount of Purchase 500
Discount Percentage 2.0%
Discount Amount 10.0
Total Amount after Discount 490.0

Single Dispatch

SingleDispatch.java

public class SingleDispatch 
{
	public void print()
	{
	  System.out.println("Single Dispatch");	
	}

 	public static void main(String[] args) 
	{
	  SingleDispatch objSingleDis = new SingleDispatch();
 	  objSingleDis.print();	
        }
}

Dynamic Dispatch
Dynamic dispatch is the same thing which we do in strategy pattern.The actual method which is called is known at the runtime

Account.java

public interface Account 
{
  public void calculateinterest();
}

SavingsAccount.java

public class SavingsAccount implements Account
{
	@Override
	public void calculateinterest() {
		System.out.println("Intrest is 8%");
	}
}

LoanAccount.java

public class LoanAccount implements Account
{
	@Override
	public void calculateinterest() {
		System.out.println("Intrest is 11.5%");
	}

}

CalculateInterest.java

public class CalculateInterest 
{
	public static void main(String[] args) 
	{
		Account objSavAcc = new SavingsAccount();
		Account objLoanAcc = new LoanAccount();
		
		objSavAcc.calculateinterest();
		objLoanAcc.calculateinterest();
	}
}

What is Multiple Dispatch
Account.java

public class Account 
{	
	public void calculateinterest() {
		System.out.println("Intrest is 11.5%");
	}
	
	public void calculateinterest(int prePayment) {
		System.out.println("Intrest is 11.5% with prePayment");
	}
	
	public void calculateinterest(int prePayment, boolean floatingIntrest) {
		System.out.println("Intrest is 11.5% with floatingIntrest");
	}
}

Now in the above example we have a Account class where the functions are overloaded.Now when the code gets executed then the methods are chosen based on the parameters passed.

public class CalculateInterest 
{
	public static void main(String[] args) 
	{
		Account objAcc = new Account();		
		
		objAcc.calculateinterest();
		objAcc.calculateinterest(23);
		objAcc.calculateinterest(23, true);
	}
}

Output

Intrest is 11.5%
Intrest is 11.5% with prePayment
Intrest is 11.5% with floatingIntrest

Now Java doesn’t supports multiple dispatch as above.The above code is an example of overloading.

Overloading vs Multiple Dispatch
The Difference between overloading and Multiple Dispatch is when the method to be called is decided at compile time then it is Overloading, if the method is decided at runtime then it is multiple dispatch

What is Double Dispatching?
In Double Dispatching the choosing of the method happens dynamically twice.In the below example the method is chosen similar to strategy pattern first time during call of viewReport method and again during choosing which printReport method to be called based on the class type its is called similar to

Staff.java

public interface Staff 
{
	void viewReport(Report objReport);
}

Teacher.java

public class Teacher implements Staff
{
	@Override
	public void viewReport(Report objReport) 
	{
                System.out.println("View Report of Teacher");
		objReport.printReport(this);
	}
}

Principal.java

public class Principal implements Staff
{
	@Override
	public void viewReport(Report objReport) 
	{		
                System.out.println("View Report of Principal");
		objReport.printReport(this);		
	}
}

Report.java

public class Report 
{
	public void printReport(Teacher objTeacher)
	{
		System.out.println("Can print report of her class"); 
	}
	
	public void printReport(Principal objPrincipal)
	{
		System.out.println("Can print report of all the class");
	}
}

ShowReport.java

public class ShowReport 
{
	public static void main(String[] args) 
	{
		Principal objPrincipal = new Principal();
		Teacher objTeacher   = new Teacher();
		objPrincipal.viewReport(new Report());
		objTeacher.viewReport(new Report());
	}
}

Output.java

View Report of Principal
Can print report of all the class
View Report of Teacher
Can print report of her class

In statically typed languages, including Java, the biggest difference between dispatch and overloading is that overloading is based on the static type of parameters (i.e. the choice of which method is actually called is decided at compile-time), while dispatch is based on the dynamic types (i.e. the decision is made at runtime). (Such languages usually don’t support multiple dispatch.)

Another Example of Double Dispatch is Serialization.In Serialization the class which is Serializable calls the methods with itself as an argument.In the below example the writeObject method dispatches the call back to the ObjectOutputStream thus making this a double dispatch. ObjectOutputStream delegates back MuxPrinter the responsibility of writing its state onto stream. By Doing this ObjectOutputStream has decoupled itself from our object objMuxPrt.

public class MuxPrinter implements Serializable
{

}

MuxPrinter objMuxPrt = new MuxPrinter();
ObjectOutputStream oos = new ObjectOutputStream();
oos.writeObject(objMuxPrt);