Categories: Java

JEE Container Managed Transaction

Dec 25, 2017 1 min.

Transaction Attribute Client’s Transaction Business Method’s Transaction Required None T2 T1 T1 RequiresNew None T2 T1 T2 Mandatory None error T1 T1 NotSupported None None T1 None Supports None None T1 T1 Never None None T1 Error Container Manager Transaction

Java Time Difference

Dec 24, 2017 2 min.

Time difference can be calculated by subtracting the milliseconds. And then milliseconds can be converted into different Units using TimeUnit. e.g. TimeUnit.MILLISECONDS.toSeconds(1000) will give 1 because 1 second equivalent to 1000 millisecons. Following sample gives a example of finding difference between two dates and converting them into different units import java.util.Date; import java.util.concurrent.TimeUnit; public class TimeDifference { public static void main(String[] args) { Date date1 = new Date(System.currentTimeMillis() - (24 * 60 * 60 * 1000)); Date date2 = new Date(); long timeDifferenceInDays = TimeUnit.

JAXRS Rest Client

Dec 20, 2017 1 min.

Option 1 : Using interface proxy import javax.ws.rs.FormParam; import javax.ws.rs.HeaderParam; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import org.apache.cxf.jaxrs.client.JAXRSClientFactory; import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; public class RestClient { public static void main(String[] args) { final MyApiClient client = JAXRSClientFactory.create("<http://localhost:8080/api>", MyApiClient.class); final HTTPClientPolicy clientPolicy = WebClient.getConfig(client).getHttpConduit().getClient(); clientPolicy.setConnectionTimeout(15 * 1000); clientPolicy.setReceiveTimeout(15 * 1000); final Response response = client.hello("your-authorization-token", "javawiki"); System.out.println("Response Status: " + response.getStatus()); } } @Path("/testapi") public interface MyApiClient { @PUT @Path("/hello") Response hello(@HeaderParam("AUTHORIZATION") String authorization, @FormParam("name") String name); } Option 2: Direct client import javax.

Java get hostname and IP address

Dec 19, 2017 1 min.

Use java.net.InetAddress to get hostname or IP Address To get local hostname or IP import java.net.InetAddress; public class LocalIPGetter { public static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.getLocalHost(); // Get Hostname System.out.println(inetAddress.getHostName()); // Get IP Address System.out.println(inetAddress.getHostAddress()); } } $javac LocalIPGetter.java $java LocalIPGetter mytestpc 172.17.0.2 To get remote hostname or IP import java.net.InetAddress; public class RemoteIPGetter { public static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.

Avoid NullPointerException on Primitive Wrapper Objects

Feb 22, 2017 5 min.

Using the primitive wrapper objects like Boolean, Integer or Long in operators may throw NullPointerException, if the value is null, because the primitive wrapper objects auto unboxed to primitive values when you use java operators like ‘==’ ‘>’ ‘<’, etc. e.g. private Boolean isDone() { return null; } private Long getMyLongValue() { return null; } public void someMethod() { // Throw NPE if (isDone()) { } // Throw NPE if (getMyLongValue() > 10) { } } This will throw NullPointerException if the method ‘isDone()’ or ‘getMyLongValue()’ returns null.

Java random integer in min and max range

Mar 4, 2016 1 min.

Following sample code returns a random integer within the give min and max range (both inclusive) import java.util.Random; public class RandomRange { // Declare as class variable so that it is not re-seeded every call private static Random random = new Random(); /** * Returns a psuedo-random number between Min and Max (both inclusive) * @param min Minimim value * @param max Maximim value. Must be greater than min. * @return Integer between min and max (both inclusive) * @see java.