Saturday, September 3, 2016

Useful New Feature Details in Java 7

To work with such data-formatted file content, you could use the DataInputStream class:



Alternatively, you can read a file using the FileReader class, which is altogether very different:

Lots of choices, lots of code. Java SE 7 does much to simplify and unify all of this.

Files, Paths class — new in Java SE 1.7. With it, you can easily create new files, create temporary files, create new (and temporary) directories, copy and move files various different ways, create symbolic links, open existing files, delete files, work with file attributes, read from files, and write to files. Just about everything you've ever wanted to do with a file can now be done through this one class
Want to open an existing file by path? Here it is:

Path path = FileSystems.getDefault().getPath(".", name);    // Paths.get("test1");
Want to read all of the bytes in that file? No more looping, here it is:

byte[] filearray = Files.readAllBytes(path);
Would you rather deal with the file line-by-line? No problem:

List<String> lines = Files.readAllLines(path, Charset.defaultCharset() );
Is it a large file that you'd rather use buffered IO with? Here you go:




BufferedReader br=Files.newBufferedReader(path,Charset.defaultCharset());       
String line = null;
while ( (line = br.readLine()) != null ) { /* … */ }
Surely, it must take more than that to create and write to file, right? Wrong: // create new,overwrite if exists


String content = …
Files.write( path, content.getBytes(),StandardOpenOption.CREATE); 

You can buffer the output and write all of the bytes with one extra line of code:



BufferedWriter-bw=Files.newBufferedWriter(path,Charset.defaultCharset(),StandardOpenOption.CREATE);
bw.write(content, 0, content.length());
the StandardOpenOption class in Java 1.7 contains all of the file and directory attributes you need to deal with file existence create, delete, overwrite, append, and other options. Again, having this all in one helper class helps a great deal.
The old and new File IO APIs can be used together:

Path path = FileSystems.getDefault().getPath(".", name);
InputStream in = Files.newInputStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
         
List<String> lines = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null)
     lines.add(line);

EJBToday2morrow : A very Brief Points


  • Simple


  • Power 

· Intercepter can intercept any lifecycle call back or Transaction services or TimerServices.
· DI –for any resources like JMS/JNDI
· If CDI enabled – deals with DI and session Bean mngmnt
Single instance per node/jvm in cluster node.
Sysadminàin mem representation of application config
Very similar to java re-entrant ReadWriteLock mechanism.
 so user provide the metadata and container takes care of the locking.
So here, fine grain locking at the required level is benefitted by the developer’s expertise on concurrency.
Initialization of the cache happen as part of the deployment

1. Using JMS to achieve the asynchronous is very heavy weight, in EJB3.1 asynchronous support is added to the Session Bean.
2. No persistent guarranty unlike JMS, as very light weight async.
3. Caller to callee no transaction propagation so end up with creating new transaction
4. No interface, bean Method get exposed asynchronously
· Either doesn’t care about return or else java 8 concurrency kind.
If cancel called it will not process further and just after completion it returns to caller.











Friday, September 2, 2016

Enums in Brief

public enum Month {
JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4), MAY(5), JUNE(6), JULY(7), AUGUST(8), SEPTEMBER(9),OCTOBER(10),NOVEMBER(11),DECEMBER(12);

    private int monthIndex;
     private Month(int monthIndex) {
        this.monthIndex = monthIndex;
    }
     public int getMonthIndex() {
        return this.monthIndex;
    }
}
Using this approach, you don’t have to worry about serialization problems as enums (are serializable by default) handles it for you. This approach is more cleaner and is considered to be the best way to implement a singleton.
public enum SessionFactory {
     SESSION_FACTORY;
     public void doSomething() {
        System.out.println("I am doing something");
    }
}
Before Java 5: a traditional enumerated type identifying weeks:
use these constants within the code for comparisons and all like if(weekDay == Weekday.SUNDAY){//do something} where weekDay is an integer var.
However weekDay may contain any other integers also like 8 or 9, as there is no restriction set by the compiler.
an enum in java:
Weekend is actually a class and SATURDAY and SUNDAY are instances of the class Weekend. Behind the scenes, the compiler converts enum Weekend {---------} into a class like 
Compiler calls the default construction to create instance as above so

Enhance an enum
assign a different behavior to each constant by introducing an abstract method into the enum and overriding this method in an anonymous subclass of the constant.

Weekend.SATURDAY.someMethod()
Enum constants are implicitly static and final and you can not change there value once created.
Currency.PENNY = Currency.DIME;  X
You can specify values of enum constants at the creation time as shown in below 
public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
But for this to work you need to define a member variable and a constructor because PENNY (1) is actually calling a constructor which accepts int value public enum Currency {
        PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
        private int value;
        private Currency(int value) {
                this.value = value;
        }
//to get the value associated with each coin you can define a 
public int getValue(){
return value
}
};   

constants defined inside Enum in Java are final you can safely compare them using "==" equality operator 
Currency usCoin = Currency.DIME;
if(usCoin == Currency.DIME){
  System.out.println("enum in java can be compared using ==");
}
Two new collection classes EnumMap and EnumSet are added into collection package to support Java Enum. These classes are high performance implementation of Map and Set interface in Java 

Enums in Brief

public enum Month {
JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4), MAY(5), JUNE(6), JULY(7), AUGUST(8), SEPTEMBER(9),OCTOBER(10),NOVEMBER(11),DECEMBER(12);

    private int monthIndex;
     private Month(int monthIndex) {
        this.monthIndex = monthIndex;
    }
     public int getMonthIndex() {
        return this.monthIndex;
    }
}
Using this approach, you don’t have to worry about serialization problems as enums (are serializable by default) handles it for you. This approach is more cleaner and is considered to be the best way to implement a singleton.
public enum SessionFactory {
     SESSION_FACTORY;
     public void doSomething() {
        System.out.println("I am doing something");
    }
}
Before Java 5: a traditional enumerated type identifying weeks:
use these constants within the code for comparisons and all like if(weekDay == Weekday.SUNDAY){//do something} where weekDay is an integer var.
However weekDay may contain any other integers also like 8 or 9, as there is no restriction set by the compiler.
an enum in java:
Weekend is actually a class and SATURDAY and SUNDAY are instances of the class Weekend. Behind the scenes, the compiler converts enum Weekend {---------} into a class like 
Compiler calls the default construction to create instance as above so

Enhance an enum
assign a different behavior to each constant by introducing an abstract method into the enum and overriding this method in an anonymous subclass of the constant.

Weekend.SATURDAY.someMethod()
Enum constants are implicitly static and final and you can not change there value once created.
Currency.PENNY = Currency.DIME;  X
You can specify values of enum constants at the creation time as shown in below 
public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
But for this to work you need to define a member variable and a constructor because PENNY (1) is actually calling a constructor which accepts int value public enum Currency {
        PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
        private int value;
        private Currency(int value) {
                this.value = value;
        }
//to get the value associated with each coin you can define a 
public int getValue(){
return value
}
};   

constants defined inside Enum in Java are final you can safely compare them using "==" equality operator 
Currency usCoin = Currency.DIME;
if(usCoin == Currency.DIME){
  System.out.println("enum in java can be compared using ==");
}
Two new collection classes EnumMap and EnumSet are added into collection package to support Java Enum. These classes are high performance implementation of Map and Set interface in Java