Solution for Exception – Could not enlist in transaction on entering meta-aware object

You will get below error when you try to access more than 1 non-XA datasource in same process in wildfly 10.

Caused by: javax.resource.ResourceException: IJ000461: Could not enlist in transaction on entering meta-aware object at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.managedConnectionReconnected(TxConnectionManagerImpl.java:560) at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.reconnectManagedConnection(AbstractConnectionManager.java:928) … 89 more
Caused by: javax.transaction.SystemException: IJ000356: Failed to enlist: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: 0:ffff7f000101:25c3e8ab:5875c454:3ec status: ActionStatus.ABORT_ONLY >
(more…)

Glassfish 4.1 : Replace default ( Moxy ) JSON-Binding Provider by Jackson 2.5.1

I switched from GlassFish 3.1.2 to GlassFish 4.1. for my project. I got some unexpected result while producing JSON since GlassFish 4.1 using Moxy as New Default JSON-Binding Provider instead of Jackson which is default for GlassFish 3.1.2. So I decided to use latest Jackson 2.5 for GlassFish 4.1 as default JSON-Binding Provider.

1.You need to register org.glassfish.jersey.jackson.JacksonFeature in your application if you want to use Jackson as your JSON provider. (by registering this, MOXy will be disabled).
Please check below code for registering JacksonFeature.

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.jackson.JacksonFeature;

@ApplicationPath("/rest")
public class MyApplication extends Application{

	private Logger LOGGER = Logger.getLogger(this.getClass().getName());
	
	@Override 
	public Set<Class<?>> getClasses() 
	{ 
		final Set<Class<?>> classes = new HashSet<>(); 
		// register root resource 
		classes.add(MyService.class); 
                
                // register JacksonFeature so that glassfish will use jackson instead of moxy.
		try
		{
		        classes.add(JacksonFeature.class);
		}catch(Exception e)
		{
			LOGGER.log(Level.SEVERE,"MyApplication : Jackson library not added.",e);
		}
		return classes; 
		
	} 
}

2. Add below jars in GlassFish’s module folder i.e “HOME/glassfish4/glassfish/modules”
– jackson-annotations-2.5.1.jar
– jackson-core-2.5.1.jar
– jackson-databind-2.5.1.jar
– jackson-module-jaxb-annotations-2.5.1.jar
– jackson-jaxrs-base-2.5.1.jar
– jackson-jaxrs-json-provider-2.5.1.jar

3. Don’t forget to restart server 🙂 Now your GlassFish 4.1 is ready to use jackson.

Happy Coding 🙂

Java – Credit Card Validation using Luhn Algorithm

I found code for credit card validation using Luhn algorithm on http://de.wikipedia.org/wiki/Luhn-Algorithmus#Java.
Thank you Wikipedia for it.
I modified code which can accept input as string & updated related changes.

public static boolean validateCardNumber(String ccNumber)
{
   int sum = 0;
   int length = ccNumber.length();
   for (int i = 0; i < length; i++) {

	   // get digits in reverse order
	   int digit = Integer.parseInt(""+ccNumber.charAt(length - i - 1));

	   // every 2nd number multiply with 2
	   if (i % 2 == 1) {
		   digit *= 2;
	   }
	   sum += digit > 9 ? digit - 9 : digit;
   }
   return sum % 10 == 0;
}
......
//it will print 'true' for below fake test card.
System.out.println(validateCardNumber("4444444444444448"));
......

Note: I hope that updated code will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Happy coding 🙂

Java Authenticator on a per connection basis.

You can perform authentication on per connection basis like below,


String userpass = username + ":" + password;
String basicAuth = "Basic " + org.apache.commons.codec.binary.Base64.encodeBase64String(userpass.getBytes("UTF-8"));
connection.setRequestProperty ("Authorization", basicAuth);

Happy Coding 🙂

TIMEOUT PROBLEM WHEN DEPLOYING GUVNOR 5.5.0 FINAL ON JBOSS-AS-7 ON WINDOWS

When I was deploying guvnor 5.5.0 on jboss-as-7.1.1.Final on windows, I got following error:

ERROR:14:22:50,023 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads – 1) JBAS015052: Did not receive a response to the deployment operation within the allowed timeout period [60 seconds]. Check the server configuration file and the server logs to find more about the status of the deployment.

After spend some time on searching, finally I solved it.

1. Go to : jboss_directory/standalone/configuration/standalone.xml

2. Search following element & add  ‘deployment-timeout’  attribute.

<deployment-scanner path=”deployments” relative-to=”jboss.server.base.dir” scan-interval=”5000″  deployment-timeout=”300″ />

Happy coding 🙂