Implementing a sever fail over feature
Suppose you have a client-server application and you want to automatically switch to a standby/backup server when the primary server is unavailable due to either failure or scheduled shut down. I present you how to achieve that by this post.We can have the primary and secondary urls in a configuration file and load them to a list at the beginning of the system. For the demonstration purpose I have hard coded list of urls.
We can achieve it easily by checking the response code sent by the server. I believe the code it self-explanatory.
public class Envision { public static void main(String[] args) throws MalformedURLException { HttpURLConnection httpURLConnection = null; InputStream inputStream = null; BufferedReader bufferedReader = null; String result = ""; String inStr = null; List<URL> urls = new ArrayList<URL>(); urls.add(new URL("http://example_primary.com")); urls.add(new URL("http://example_secondary.com")); try { for (int i = 0; i < urls.size(); i++) { try { httpURLConnection = (HttpURLConnection) urls.get(i).openConnection(); inputStream = httpURLConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); int responseCode = httpURLConnection.getResponseCode(); if (responseCode == 200) { while ((inStr = bufferedReader.readLine()) != null) { result = result + inStr; } break; } } catch (Exception e) { System.out.println("Error: While requesting data from server "+urls.get(i)+" " + e.getMessage() ); } } System.out.println(">>>>Res: " + result); } catch (Exception e) { System.out.println("Error: While requesting data from server" + e.getMessage()); } } }
Happy Coding!
How to create a batch file to run a Java program?
I demonstrated how to create an .exe using maven in my previous post. Another way of distributing a software is by a run time which includes a batch file.What is a batch file?
A batch file is a type of script file which contains a series of instructions to be executed in turn. These are used to automate frequently performed tasks.
You can write a batch file to compile, to create the JAR file and run the program. But in this post I mostly focus on creating run time and I assume that you have the .jar file already with you. You can use a tool like Maven or Ant to build your .jar file.
The following image shows the folder structure of my run time.
Folder Structure of the runtime |
Then let's create the start.bat file. Actually what you have to do is very easy. Just place following lines in a text file and save it with the extension of .bat.
title=XML Reader java -Xmx256m -DAlert=true -classpath .;Blog-1.0-SNAPSHOT.jar;.\lib\jdom-2.0.5.jar Envision pause
As you can see I have given the name of the JAR file, libraries to be used in the program (if you have many libraries give them as a series separated by semi colon ) and at the end the name of the main class.
It's really easy to make a run time, isn't it?
Happy coding!
How to create an exe for a java program using maven?
Usually a software is distributed as an executable file. Therefore as programmers we would need to create an .exe file for our programs. Through this post I will present you how to achieve it easily with Maven (a build automation tool used primarily for Java projects) .A windows executable can be created by using a combination of two maven plug-ins , Maven Shade plugin and launch4j plugin.
Here is my program. It is to read an .xml file and write its content to the standard out put. In order to read the .xml file we have to use a library. Here I have used jdom2. Like that in developing software we have to depend on many libraries in order to prevent reinventing the wheel. If we are building our projects using maven we can include those dependencies in the pom.xml file.
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import java.io.File; import java.io.IOException; import java.util.List; public class Envision { public static void main(String[] args){ File xmlFile = new File("D:\\example.xml"); SAXBuilder builder = new SAXBuilder(); try { Document document = (Document) builder.build(xmlFile); Element rootNode = document.getRootElement(); List books = rootNode.getChildren("book"); System.out.println("This is my book store"); for (int l = 0; l < books.size(); l++) { Element book = (Element) books.get(l); System.out.println("Name :"+book.getChildText("name")+" Author :"+book.getChildText("author")); System.out.println("----------------------------------------------------------------------"); } } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } } }
Here is my pom.xml file. Here Maven Shade plugin is used to add all the dependencies in the program into the runnable jar file. The launch4j creates the .exe with vender information and a nice icon too.
I have added the exe.ico in src/main/resources.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Blog</groupId>
<artifactId>Blog</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>0.0.6-BETA</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Envision</mainClass>
</transformer>
</transformers>
</configuration>
</plugin>
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<!-- Command-line exe -->
<execution>
<id>l4j-cli</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<outfile>target/envision.exe</outfile>
<jar>target/${artifactId}-${version}.jar</jar>
<errTitle>App Err</errTitle>
<classPath>
<mainClass>Envision</mainClass>
</classPath>
<icon>src/main/resources/exe.ico</icon>
<jre>
<minVersion>1.5.0</minVersion>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>${project.version}</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<copyright>2014 envision.com</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>1.0.0.0</txtProductVersion>
<productName>${project.name}</productName>
<companyName>Envision</companyName>
<internalName>envision</internalName>
<originalFilename>envision.exe</originalFilename>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After configuring the pom.xml file just execute maven install to get the .exe file.
That's it. Check the target folder in you project folder to find the .exe.
Hope this would help you :)
Happy Coding!
Subscribe to:
Posts
(
Atom
)
4 comments :
Post a Comment