Posts

Showing posts from 2015

Java RMI (Remote Method Invocation)

Image
Hi, now I am gonna show you how to write java RMI process to create server and client. from oracle documentation "Java Remote Method Invocation (Java RMI) enables the programmer to create distributed Java technology-based to Java technology-based applications, in which the methods of remote Java objects can be invoked from other Java virtual machines*, possibly on different hosts. RMI uses object serialization to marshal and unmarshal parameters and does not truncate types, supporting true object-oriented polymorphism." So from here, we can easily understand that java rmi server can be located in remote or same jvm host. we can call the remote method from a client that can be located different hosts or same from sever. so what is the difference between "Web Service" and "Java RMI" as both are called from remote client and send responses to the client. Well, the difference is "Java RMI" server and client both should be java process that mean

Java Message Service (JMS)

Image
Java message service is pretty good to send message to another. its maintain a queue and when any message stay in that queue, the publisher immediately publish the message. From the EJB (Enterprise Java Beans) context, generally JMS is used for distributed asynchronous computing. Also its can be used for debugging, web chat service for customer and the provider etc. here i used it for doing logging for web service operation. the response can not wait for log write rather response return to the user and the log message sent to the HornetQ (a message Queue). then log will insert to database. Here i used JPA for query and Bean managed transaction. necessary tools given below, 1) JBoss-eap-6.2 or 7 2) MySql DB. here is the project structure. first we need to add the HornetQ module and for this make a xml file say named "hornetq-jms.xml" and put the code here. put the xml file in META-INF folder in your class path. <?xml version="1.0" encoding="U

Google GeoCoding

Geo-coding means we are now working with something location based. that's great. Google make it easy by providing the service. we can found any location based information the URL given below: 1: http://maps.googleapis.com/maps/api/geocode/json here the last portion for which format response you want?? json or xml? just make your choice, if we want response in xml format then put 'xml' instead of  'json'. we also include required parameter in the url. we can search a location by name or longitude, latitude based and many more. we are going to show example of both name and longitude, latitude based. Here i use jackson object mapper to mapped the response. you need to add the jar in your class path. oh, i forgot to say add commons.io.2.4 jar or upper version (i don't know does the upper version already exits. :P ). you can find jackson jar from  here . here function that give the longitude and latitude given below, 1: public GoogleResponse conv

Cron Job (Scheduler)

Sometimes we need to run a class after a specific time again and again. we can called this say scheduler. wow, so it can run after a periodic time. suppose everyday at 10 pm we need to send email to our registered consumers. so we need to run our MailSend class file ( assume ) everyday at 10pm. it's just an scenario. in java this is called cron job. we can achieve this by 1) using quartz scheduler. 2) using cron4j. 3) using spring jar. etc whatever, we can use any of them. here i used quartz scheduler. to use it, first download the stable version of quartz jar from  here . then add it to your class path. suppose i have a class that is for just to connect the database. this class i want to run after 10 sec again and again. the class is given below, 1: import java.io.FileInputStream; 2: import java.io.FileNotFoundException; 3: import java.io.IOException; 4: import java.io.InputStream; 5: import java.sql.Connection; 6: import java.sql.DriverManager; 7:

UUID to BigInteger conversion and BigInteger to UUID conversion

Some times we need to convert UUID to BigInteger and vice versa. I searched a lot and it takes my time. Here you can get BigInteger from UUID easily. but when you want to get the same UUID, then it would be a little bit tricky. First you need to convert the BigInteger no to Hex and then append '-' into uuid. that's it. but what happen if you get a negative BigInteger number during conversion?? then exception caught during conversion from hex to UUID. so you need to return a positive bigint number during conversion. java BigInteger class has a constructor which takes two parameter. the first one defined 1, 0, or -1. 1 defines that it always return a positive that means no leading '-'ve sign in the bigint number and the second one byte[ ] for conversion the actual number. this method can give us a positive BigInteger private static BigInteger getBigIntegerFromUuid(UUID randomUUID) { ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(ran