Monday, September 16, 2013

Tuesday, August 6, 2013

Copying files over network shared folder using Java

Problem :: One of my blog reader posted a problem that he cannot copy files over a network shared folder using java.

Solution :: Java alone doesn't have support for networked file sharing. But if you use a library, like Samba, then you can. 

You can check below source code which successfully copies file from my local drive to a shared network folder ::

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkupload;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import jcifs.Config;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

/**
 *
 * @author ujjwal
 */
public class NetworkUpload {

     public static void main(String[] args) throws IOException {
  final String userName = "Ujjwal";
  final String password = "ujjwal12345";
  final String domain = "ujjwal12345";
  //source path begins with / as i am using macbook (mac osx), in your case it will be windows drive path
  final String sourcePath = "/Developer/test.pdf";
  final String destinationPath = "smb://192.168.60.14/ftp/test.pdf";

  copyFileUsingJcifs(domain,userName, password, sourcePath, destinationPath);

  System.out.println("The file has been copied using JCIFS");
 }

 public static void copyFileUsingJcifs(final String domain,final String userName,
   final String password, final String sourcePath,
   final String destinationPath) throws IOException {


  final NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
    domain, userName, password);
  final SmbFile sFile = new SmbFile(destinationPath, auth);
  final SmbFileOutputStream smbFileOutputStream = new SmbFileOutputStream(
    sFile);
  final FileInputStream fileInputStream = new FileInputStream(new File(
    sourcePath));

  final byte[] buf = new byte[16 * 1024 * 1024];
  int len;
  while ((len = fileInputStream.read(buf)) > 0) {
   smbFileOutputStream.write(buf, 0, len);
  }
  fileInputStream.close();
  smbFileOutputStream.close();
 }
  
}

If you face any issues implementing this code, you can contact me ::
Cheers,
Ujjwal Soni

Monday, August 5, 2013

Useful Java Utilities and Frameworks

There are so many useful Java frameworks and utilities out there that are free and open source that it boggles the mind. Here are a few of my recent favorites. Feel free to add your own to the list. There are many many more that I did not add to this list because they are very common (e.g. log4J, JUnit, etc.)
  • Joda Time Java Date/Time replacement
  • XStream “A simple library to serialize objects to XML and back again.”
  • Rome Java tools for parsing, generating and publishing RSS and Atom feeds.
  • Google Collections “a suite of new collections and collection-related goodness for Java 5.0, brought to you by Google. This library is a natural extension of the Java Collections Framework you already know and love. “
  • Google Guice (DI Framework) “Put simply, Guice alleviates the need for factories and the use of new in your Java code. Think of Guice’s @Inject as the new new. You will still need to write factories in some cases, but your code will not depend directly on them. Your code will be easier to change, unit test and reuse in other contexts.”
  • EasyMock “EasyMock provides Mock Objects for interfaces in JUnit tests by generating them on the fly using Java’s proxy mechanism.”
  • Apache Commons Tons of useful utilities for Java that I can’t pick just one.
  • GWT Java-Ajax web framework – very cool
  • JFreeChart “JFreeChart is a free 100% Java chart library that makes it easy for developers to display professional quality charts in their applications.”
  • Java Mozilla HTML Parser a wrapper around Mozilla’s HTML Parser.
  • Lucene “High-performance, full-featured text search engine library written entirely in Java.”
  • Squirrel SQL “Graphical Java program that will allow you to view the structure of a JDBC compliant database, browse the data in tables, issue SQL commands etc,”
  • NIO.2 Ok, technically not an open source framework. This is the new version of NIO that will be included in Java 7. This is what we’ll be using in place of File and many of the other classes in the java.io package. Pretty cool.

Tuesday, July 9, 2013

SQL Server 2012 Connection using Java Source code written in Jdk 1.4

I  tried many Jdbc drivers but all failed giving errors like "Unsupported Major Minor Version".  Later i found a cool driver which allows connection smoothly using jdk 1.4 source code and it compiles quite well, its also better in comparision to other drivers in terms of performance.

JSQLConnect JDBC_2.0_Driver connects fine using jdk 1.4, 1.5 source code.

If you need help/alternate, comment this post or email me.

Cheers,

Ujjwal Soni

Back-porting jtds driver to jdk 1.4

Recently, i had to connect to MS Sql server 2012 using source code compiled under jdk 1.4, so, i back-ported jtds driver to jdk 1.4, backporting process went fine but when i tried to connect it to MS Sql server 2012 using my java code, it entered into endless non-responsive loop. So, i gave up this back-porting process.

Java Secret: What is called before main.

Overview

The starting point for core java applications is the main(String[]) method. But is there any code which is called before this method and do we even need it?

The class has to be initialised

Before calling main, the static block for the class is called to initialise the class.

public class Main {
    static {
        System.out.println("Called first.");
    }
    public static void main(String... args) {
        System.out.println("Hello world.");
    }
}
prints
Called first.
Hello world.

Can we avoid having a main()

Normally, if you don't have a main() method, you will get an error. However if your program exits before calling main() no error is produced.
public class Main {
    static {
        System.out.println("Hello world.");
        System.exit(0);
    }
}
prints
Hello world.

The premain method

If you have Java agents, those agents can have a premain method which is called first. Instrument package

public static void premain(String agentArgs, Instrumentation inst);
The Instrumentation class gives the agent access to each class' byte code after it is read and before it is linked, giving the agent the option to change byte code.

One interesting feature of the Instrumentation class is the getObjectSize() which will give you the size of an object.

High performance libraries in Java



There is an increasing number of libraries which are described as high performance and have benchmarks to back that claim up. Here is a selection that I am aware of.

Disruptor library

http://code.google.com/p/disruptor/

LMAX aims to be the fastest trading platform in the world. Clearly, in order to achieve this we needed to do something special to achieve very low-latency and high-throughput with our Java platform. Performance testing showed that using queues to pass data between stages of the system was introducing latency, so we focused on optimising this area.
The Disruptor is the result of our research and testing. We found that cache misses at the CPU-level, and locks requiring kernel arbitration are both extremely costly, so we created a framework which has "mechanical sympathy" for the hardware it's running on, and that's lock-free.
The 6 million TPS benchmark was measured on a 3Ghz dual-socket quad-core Nehalem based Dell server with 32GB RAM.
http://martinfowler.com/articles/lmax.html

Java Chronicle

https://github.com/peter-lawrey/Java-Chronicle
This library is an ultra low latency, high throughput, persisted, messaging and event driven in memory database. The typical latency is as low as 16 nano-seconds and supports throughputs of 5-20 million messages/record updates per second.
It uses almost no heap, trivial GC impact, can be much larger than your physical memory size (only limited by the size of your disk). and can be shared between processes with better than 1/10th latency of using Sockets over loopback.

It can change the way you design your system because it allows you to have independent processes which can be running or not at the same time (as no messages are lost) This is useful for restarting services and testing your services from canned data. e.g. like sub-microsecond durable messaging.
You can attach any number of readers, including tools to see the exact state of the data externally. e.g. You can use; od -t cx1 {file} to see the current state.

Colt Matrix library

http://acs.lbl.gov/software/colt/
Scientific and technical computing, as, for example, carried out at CERN, is characterized by demanding problem sizes and a need for high performance at reasonably small memory footprint. There is a perception by many that the Java language is unsuited for such work. However, recent trends in its evolution suggest that it may soon be a major player in performance sensitive scientific and technical computing. For example, IBM Watson's Ninja project showed that Java can indeed perform BLAS matrix computations up to 90% as fast as optimized Fortran. The Java Grande Forum Numerics Working Group provides a focal point for information on numerical computing in Java. With the performance gap steadily closing, Java has recently found increased adoption in the field. The reasons include ease of use, cross-platform nature, built-in support for multi-threading, network friendly APIs and a healthy pool of available developers. Still, these efforts are to a significant degree hindered by the lack of foundation toolkits broadly available and conveniently accessible in C and Fortran.

The latest stable Colt release breaks the 1.9 Gflop/s barrier on JDK ibm-1.4.1, RedHat 9.0, 2x IntelXeon@2.8 GHz.

Javolution

http://javolution.org/ Javolution real-time goals are simple: To make your application faster and more time predictable! That being accomplished through:
  • High performance and time-deterministic (real-time) util / lang / text / io / xml base classes. 
  • Context programming in order to achieve true separation of concerns (logging, performance, etc). 
  • A testing framework addressing not only unit tests but also performance and regression tests as well. 
  • Straightforward and low-level parallel computing capabilities with ConcurrentContext. Struct and Union base classes for direct interfacing with native applications (e.g. C/C++). 
  • World's fastest and first hard real-time XML marshalling/unmarshalling facility. Simple yet flexible configuration management of your application. 

Trove collections for primitives

http://trove.starlight-systems.com/
The Trove library provides high speed regular and primitive collections for Java.
The GNU Trove library has two objectives:
  • Provide "free" (as in "free speech" and "free beer"), fast, lightweight implementations of the java.util Collections API. These implementations are designed to be pluggable replacements for their JDK equivalents.
  • Provide primitive collections with similar APIs to the above. This gap in the JDK is often addressed by using the "wrapper" classes (java.lang.Integer, java.lang.Float, etc.) with Object-based collections. For most applications, however, collections which store primitives directly will require less space and yield significant performance gains.

MG4J: Managing Gigabytes for Java™

http://mg4j.dsi.unimi.it/
MG4J (Managing Gigabytes for Java) is a free full-text search engine for large document collections written in Java. MG4J is a highly customisable, high-performance, full-fledged search engine providing state-of-the-art features (such as BM25/BM25F scoring) and new research algorithms.

Other links

Overview of 8 performance libraries

http://www.dzone.com/links/r/8_best_open_source_high_performance_java_collecti.html
Sometimes collection classes in JDK may not sufficient. We may require some high performance hashtable, Bigarrays etc. Check out the list of open source high performance collection libraries.

Serialization benchmark

http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
This is a comparison of some serialization libraries.
Its still hard to beat hand coded serialization.