Java

Using Selenium Chrome Driver on Mac OS X

Update (26/06/2015)
Previously I have written a function which waits until a specific element is available on the page, doWhenAvailable. I have find out Selenium has already a similar feature build-in. Its element search functions could wait until an element is available on the page. It can be activated in @BeforeClass method:

driver. manage( .timeouts( ). implicitlyWait(TIMEOUT_TIME,  TimeUnit.SECONDS) ;
//doWhenAvailable(driver, By.id( “view_profile”), WebElement::click); 
driver.findElement(By.id(“view_profile”)).click() ;

 

It took several attempts for me able to use Chrome Driver by using the instructions from its website and I have decided to publish a step by step tutorial.

Selenium allows you to run your tests using several different browsers Chrome, Firefox, InternetExplorer and HTMLUnit. HTMLUnit is not a real browser it is just a browser simulator written in Java, it can execute JavaScript too. Some settings you have to use for each one is different but for all of them you have to start by adding main Selenium dependency to you project. This is the common part.

Creating Maven Project

I have created a new maven project using following pom.xml but you can just add selenium dependency to your own pom.xml if you want. Here is the pom.xml file I have used for my test project:

 

<!--?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>com.deniz.jira</groupid>
    <artifactid>JIRASeleniumTests</artifactid>
    <version>1.0</version>
    <name>JIRASeleniumTests</name>
    <dependencies>
        <!-- This is only required dependency. -->
        <dependency>
            <groupid>org.seleniumhq.selenium</groupid>
            <artifactid>selenium-java</artifactid>
            <version>2.46.0</version>
        </dependency>
        <!-- Selenium does not enforce any unit testing framework and
        it does not have any unit testing features, like asserts so
        we are using JUnit for this. But this is optional and
        you do not need to use any unit testing library if you want. -->
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.hamcrest</groupid>
            <artifactid>hamcrest-all</artifactid>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <!-- Using Java 8's Lambda expressions makes code simple.
        Because you will be using a lot of handler to asynchronous methods.
        But this is not required. -->
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <version>3.3</version>
                <configuration>
                    <source>1.8
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

WebDriver Specific Initialization

For newcomers it is best to stick with Firefox. It is easier to setup. After installing Firefox browser to you system just create a new FirefoxDriver and start running your project. There are no extra step required.

For Chrome in addition to Chrome Browser you have to install Chrome Driver too. You can download Chrome Driver from Chromium web site and put it in a suitable place in your system. Location is not important because we will be providing location of it through a system property to our test execution.

In my case I have put chromedriver executable to Applications folder. So for my JUnit test I have use following setting:

System.setProperty("webdriver.chrome.driver" ,  "/Applications/chromedriver");

Writing Actual Test Codes

We have to initialize our WebDriver instance, since I am using JUnit I have done it in @BeforeClass so it is automatically run before my test cases. You can also do this before every test case using @BeforeTest but it will be a lot slower because you will most probably do extra initialization than shown below. For example logging-in to web site.

@BeforeClass
public static void init() {
    driver = new ChromeDriver(); //init chrome driver
    //driver = new FirefoxDriver(); //uncomment to use Firefox
    driver.get("http://localhost:8080”); //url of website we like to test
    driver.manage().window().setSize(new Dimension(1600, 900)); //resize browser window
    //login to web site
}

You will most probably want browser to automatically closed after your tests are completed. Add driver.quit() to your JUnit teardown method. If you do not each execution will left a new browser window open.

@AfterClass
public static void teardown() {
    driver.quit();
}

Nowadays nearly all web applications will be loaded asynchronously. That mean when page is loaded the element you like to test will not be on the page, it will be generated by a JavaScript code. This makes writing test a little more difficult, you have to wait until related element is created by JavaScript code. I have writing a small helper function and reused it in my test code. It waits until given element (Specified using By) is available on the page and execute the given code block (Consumer) when the element is available:

public   static   void   doWhenAvailable( WebDriver driver ,  By by ,  Consumer consumer )   { 
     ( new   WebDriverWait (driver,  TIMEOUT_TIME ) ).until( ( WebDriver drv )   -&gt;  driver.findElement( by )  !=  null ) ; 
    consumer.accept( driver.findElement( by ) ) ; 
 }

 

Using this method with Java 8 is very readable. For example below code waits until an element with id=“view_profile” is available on the page and when it is available just clicks on it.

doWhenAvailable(driver, By.id("view_profile"), WebElement::click);

Following code performs more complex operation when requested element is available. It wails until <select id=“ts_month_picker_selector” …..> select element is available on the page and select option element which has a value of “Jan”:

doWhenAvailable(driver, By.id("ts_month_picker_selector"), (element) -&gt; {
    Select select = new Select(element);
    select.selectByValue(String.valueOf(“Jan"));
});

You will mostly need to perform other asynchronous operations inside the callback and this will result into callback hell. I am still looking a way to promise style of chaining asynchronous operations.

Tips and Tricks

  • Give every HTML element a unique id field. This will allow you to specify elements easily in your test code.
  • Create a reusable small test library specific to your application. For example you should encapsulate logging-in to test system in a method and reuse that method from every test case.
  • Try to avoid time based waits with Thread.sleep() or any other sleep as much as possible. Because they will increate time to run your tests. But you will probably need this for some AJAX based code. In my case I need it for dynamically filled comboboxes which fills its content as you type. Otherwise you may get StaleElementReferenceException because your driver will be using a previous instance of the same visual element not the one on the page. Instead of sleep you may allow StaleElementReferenceException to occur and re-fetch the element automatically. You can check details from here.
  • At the end of the test if test fails take a screenshot automatically. I will explain it in another post. 
VN:F [1.9.22_1171]
Rating: 7.7/10 (3 votes cast)

Best eclipse shortcut

I have been using eclipse for years. I were using several shortcuts to speed up things. But I have learned my favorite shortcut, CTRL+3 just a few months ago. It is something like one shortcut to learn them all. How could I miss this for years?

VN:F [1.9.22_1171]
Rating: 6.0/10 (1 vote cast)

New Features of JDK 7 Explained

For an in company JDK 7 orientation I have prepared a presentation which summaries new features of JDK 7. I would like to make it public for everyone who needs it.

New Features of JDK 7 is available on slideshare.

You can also download pdf of it from here.

New Features Of JDK 7 — Presentation Transcript
1. Java Reloaded JDK 7 by Deniz Oğuz
2. Objective Learn what is included in JDK 7 and remember to use Google when required
3. JDK Release History Version Release Date JDK 1.0 1996-01-23 JDK 1.1 1997-02-18 JDK 1.1.4 1997-09-12 JDK 1.1.5 1997-12-03 JDK 1.1.6 1998-04-24 JDK 1.1.7 1998-09-28 • Normal release cycle was 2 years, J2SE is 3 JDK 1.1.8 1999-04-08 J2SE 1.2 1998-12-04 years late. It should have bean released at the J2SE 1.2.1 1999-03-30 end of 2008. J2SE 1.2.2 1999-07-08 J2SE 1.3 2000-05-08 • Some of the most wanted features are J2SE 1.3.1 2001-05-17 postponed to J2SE 8. J2SE 1.4.0 2002-02-13 J2SE 1.4.1 2002-09-16 J2SE 1.4.2 2003-06-26 J2SE 5.0 2004-09-29 J2SE 6 2006-12-11 Future Releases J2SE 7 2011-07-28 J2SE 8 Expected in late 2012
4. How to Try JDK 7 Features?1. Download JDK 7 build from jdk7.java.net  (download zipped version and extract to a folder)2. Download Netbeans 7  (download SE version, 80 MB)3. In the IDE, choose Tools > Java Platforms from the main menu4. Click Add Platform and specify the directory that contains the JDK5. Ensure JDK 1.7 is chosen in the Platforms list and click Close6. On Project Properties Libraries section Select JDK 1.7 as Java     Platform7. On Project Properties Sources section Select JDK 7 as Source/Binary Format
5. New Javadoc Format
6. Project Coin String in switch statement Binary literals Underscore in literals Diamond operator Improved exception handling Try with resource Simplified vararg methods invocation
7. String in Switch Statement Before JDK 7 only byte, short, char, int were allowed in switch statements JDK 7 allows Strings to be used in switch statements Why isn’t long supported in switch statements? Console console = System.console(); String day= console.readLine(); switch (day) { case “monday” : console.writer().write(“1”);break; case “tuesday” : console.writer().write(“2”);break; case “wednesday” : console.writer().write(“3”);break; case “thursday” : console.writer().write(“4”);break; case “friday” : console.writer().write(“5”);break; case “saturday” : console.writer().write(“6”);break; case “sunday” : console.writer().write(“7”);break; default:console.writer().write(“?”); } console.flush();
8. Binary Literals It is now easier to specify numbers in binary form  int mask = 0b00000000000000000000000011111111;  int mask = Integer.parseInt(“00000000000000000000000011111111”, 2);  int mask = 255;
9. Underscores in Numbers int money = 100_000_000; long creditCNumber = 3434_3423_4343_4232L; int mask = 0b0000_0000_0000_0000_0000_0000_1111_1111;
10. Diamond Operator Before JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<Integer, Track>(); With JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<>();
11. Multi Catch and Final Rethrow Problem : You want to handle multiple exceptions with the same code block.try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException ex) {     log.warn(“Can not access file”, settingFile); } catch (FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException | FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }
12. Multi Catch and Final Re-throw Cont. Problem : You want to handle multiple exceptions with the same code block.public Setting readSettings(Strin settingFile) throws ParseException, IOException,FileNotFoundException { try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (Throwable ex) { log.warn(“Can not read settings”, settingFile); throw ex; } ……………..}
13. Try With Resourceprivate static String readConfiguration(String file) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } finally { if (reader != null) { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
14. Try With Resource Cont’private static String readConfigurationNew(String file) { try (BufferedReader reader = new BufferedReader(new FileReader(file));) { String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } }
15. Try With Resource and Autocloseable A new interface AutoCloseable is introduced Existing Closeable interface is changed to extend AutoCloseable interface A new method addSuppressed(Exception) is added to Throwable Exceptions throwed from close method of AutoCloseable are suppressed in favor of exceptions throwed from try-catch block See JavaDoc of Autocloseable for more detail
16. Autocloseable and Suppressed ExceptionExceptions from try-catch body suppresses exceptions thrown from AutoCloseable.close public ABCResource implements AutoCloseable { @Override public void close() throws ABCException { dosomething(); if (errorCondition) { throw new ABCException(“Not supported yet.”); } } } public ClientClass { public void useABCResource() { try (ABCResource resource = new ABCResource();) { dosomething(); if (errorCondition) { throw new ClientException(“Not supported yet.”); } } } }
17. Better Support for Other Languages in JVM  Statically Typed vs Dynamically Typed  Compile time type checking vs runtime type checking  Java, C, Scala are examples of statically typed languages  Java Script, Ruby are examples of dynamically typed languages  Strongly Typed vs Weakly Typed  Automatic type conversion as necessary vs fixed type  Java Script is weakly typed language  Java is a strongly typed languagepublic void printTotal(a, b) { print a + b;}public void printTotal(int a, int b) { print a + b;}
18. Dynamic Typing was Difficult to Implement on JVM• New bytecode is introduced • invokeinterface • invokestatic • invokevirtual • invokespecial • invokedynamic (Only bytecode in JVM that is not used by Java PL)• Execution environment of the programming language provides a bootstrapmethod for resolving method invocations
19. Fork/Join Framework Uses work-stealing algorithm Task is broken into smaller parts recursively A new ExecutorService implementation ForkJoinPool is added ForkJoinTask and its subclasses RecursiveAction and RecursiveTask are added
20. General Usage Pattern of Fork/JoinResult compute(Problem problem) { if (problem is small) directly solve problem else { split problem into independent parts fork new subtasks to solve each part join all subtasks compose result from subresults }else { RecursiveTask left = new ComputationTask(array, low, mid); RecursiveTask right = new ComputationTask(array, mid, high); right.fork(); return left.compute() + right.join();}main { RecursiveTask computationTask = new ComputationTask(array, 0, array.length); ForkJoinPool mainPool = new ForkJoinPool(); Long result = mainPool.invoke(computationTask);}
21. Work Stealing Less thread contention Improved data locality Execute large tasks early
22. TransferQueue Allows producers to wait until message is processed by a consumer even if the queue is not    full. transfer(E e)  Transfers the element to a consumer, waiting if necessary to do so. tryTransfer(E e)  Transfers the element to a waiting consumer immediately, if possible. tryTransfer(E e, long timeout, TimeUnit unit)  Transfers the element to a consumer if it is possible to do so before the timeout elapses. getWaitingConsumerCount()  Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timed poll hasWaitingConsumer()  Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take () or timed poll.
23. ThreadLocalRandom A random number generator isolated to current thread Aim is to reduce contention in multi threaded environments Usage:  ThreadLocalRandom.current().nextInt(min, max);  ThreadLocalRandom.current().nextLong(min, max);  ThreadLocalRandom.current().nextDouble(min, max);
24. ConcurrentLinkedDeque Unbound concurrent deque based on linked nodes Concurrent insertion, removal, and access operations execute safely across multiple threads Iterators are weakly consistent and do not throw ConcurrentModificationException size() method is NOT constant in time Bulk operations are not guaranteed to perform atomically
25. Phaser An reusable synchronization barrier Similar to CyclicBarrier or CountDownLatch but with more advance features :  Allows number of registered parties to change after Phaser creation  Each generation increment phase number of phaser  There are blocking and non blocking versions of operations Extremely flexible and complex, use Javadoc when you need to use this class
26. SCTP Support Feature SCTP TCP UDPConnection-oriented  Full duplex   Reliable data transfer  Partial-reliable data transfer optionalOrdered data delivery  Unordered data delivery  Flow control  Congestion control  ECN capable  Selective ACKs  optionalPreservation of message boundaries  Path MTU discovery  Application PDU fragmentation  Application PDU bundling  Multistreaming Multihoming Protection against SYN flooding attacks  n/aAllows half-closed connections  n/aReachability check  Psuedo-header for checksum (uses vtags)  Time wait state for vtags for 4-tuple n/a • Not all operating systems support SCTP
27. The Need for Java NIO.2 Methods works inconsistently  Delete method sometimes can not delete  Rename method sometimes can not rename No Exception is thrown from failed method Accessing metadata of files is limited Does not scale well  Listing a directory may take a long time (especially over network directories) A change notification facility is not provided Developers wanted to create their own file system implementations  For example an in-memory file system
28. Pluggable FileSystems java.nio.file.FileSystems is factory for file systems. Implement java.nio.file.spi.FileSystemProvider to provide your own file systems  A filesystem provider for Zip and Jar files are included in JDK 7  You can implement a filesystem to open a ISO image as a file system, or implement a RAM disk etc…… java.nio.file.FileSystems.getDefault() is used most of the time Multiple/Alternate views of same underlying files  Hides some files for security, read-only view, etc.
29. java.nio.file.Path and java.nio.file.Files Use java.nio.file.Path and java.nio.file.Files instead of java.io.File  Use java.io.File.toPath and java.nio.file.Path.toFile methods to integrate with legacy code java.nio.file.Paths contains factory methods for java.nio.file.Path  static Path getPath(String first, String… more)  static Path getPath(URI uri) Once you obtained Path object use java.nio.file.Files static methods to process  copy, createLink, createTempFile, delete, exist, getPosixFilePermissions, getAttribute, isHidden, isExecutable, isSymbolicLink, newByteChannel ……
30. Asynchronous I/O Allows application to continue on something while waiting for I/O Asynchronous I/O operations will usually take one of two forms:  Future<V> operation(…)  void operation(… A attachment, CompletionHandler<V,? super A> handler) See classes AsynchronousXXXXChannel that extends AsynchronousChannel Path path = Paths.get(“/home/deniz/dropbox/Getting Started.pdf”); AsynchronousFileChannel ch = AsynchronousFileChannel.open(path); ByteBuffer buf = ByteBuffer.allocate(1024); Future<Integer> result = ch.read(buf, 0); //read does not block while (!result.isDone()) { System.out.println(“lets do something else while waiting”); } System.out.println(“Bytes read = ” + result.get()); //Future.get will block ch.close();
31. Watch Service WatchService allows you to monitor a watchable object for changes and events. It implements Reactor pattern. Just like Selector does for Channels. Use Watchable.register to register with java.nio.file.WatchService to listen changes WatchKey register(WatchService watcher, WatchEvent.Kind<?>… events)  WatchEvent.Kind is an interface, actual applicable alternatives depends on Watchable Example Watchable objects  Path, FileSystem Path path = Paths.get(“C:/Users/Deniz/Dropbox/”); WatchService watcher = path.getFileSystem().newWatchService(); while (true) { path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); WatchKey key = watcher.take(); // block for event for (WatchEvent event : key.pollEvents()) { Path pathOfNewFile = (Path) event.context(); // path of new file System.out.println(“File is created:” + pathOfNewFile.toString()); } key.cancel(); }
32. FileVisitorPath start = …Files.walkFileTree(start, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE;   } public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } }});
33. FileTypeDetector Files.walkFileTree(Paths.get(“C:/Users/Deniz/Downloads”), new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileType = Files.probeContentType(file); System.out.println(file.toString() + “:” + fileType); return FileVisitResult.CONTINUE; } });Output:C:UsersDenizDownloadsWirofon-Client-0.2.10-rc01.exe:application/x-msdownloadC:UsersDenizDownloadsPlan B-She Said.mp3:audio/mpegC:UsersDenizDownloadsROM or Kernel-Update.pdf:application/pdf
34. Xrender Pipeline Will replace OpenGL pipeline on Unix, Linux platforms  Due to poor OpenGL drivers OpenGL pipeline has several problems  A lot of drivers has optimized Xrender implementations  Xrender is more suited 2D applications than OpenGL  Xrender applications are native X11 applications  Other GUI libraries are also using Xrender for 2D effects  QT4, GTK+, KDE4 Use -Dsun.java2d.xrender=true to enable it
35. Translucent Windows Java SE 6u10 introduced com.sun.awt.AWTUtilities to support translucent and shaped windows  AWTUtilities is removed in JDK7  Simple translucencysetUndecorated(true);setOpacity (opacityValue); //opacity value is between 0.0 – 1.0 Per-Pixel translucencyColor colorA = new Color (255, 0, 0, 0); // Transparent redColor colorB = new Color (255, 0, 0, 255); // Solid redsetBackground(new Color (0, 0, 0, 0)); // activate per-pixel  Transparent and shaped windows // translucency.protected void paintComponent (Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp = new GradientPaint ( 0.0f, 0.0f, colorA, 0.0f, getHeight (), colorB, true); g2d.setPaint (gp); g2d.fillRect (0, 0, getWidth (), getHeight ());}
36. Shaped Windows  Shaped windows  Only the parts that belong to the given Shape remain visible and clickable  A shaped window can also be translucent if you wantsetUndecorated(true);setShape(new Ellipse2D.Float (0, 0, getWidth (), getHeight ()));For code samples see: Exploring JDK 7, Part 2: Translucent and Shaped Windows
37. JLayer and LayerUI Similar to GlassPane but it is a layer around any JComponent not only JRootPane You can mask mouse events in installUI methodLayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();……JFormattedTextField dateField = new JFormattedTextField(dateFormat);JFormattedTextField numberField = new JFormattedTextField(numberFormat);……JPanel datePanel = new JPanel();……datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));datePanel.add(new JLayer<JFormattedTextField>(numberField , layerUI));……class ValidationLayerUI extends LayerUI<JFormattedTextField> { public void paint (Graphics g, JComponent c) { super.paint (g, c); JLayer jlayer = (JLayer) c; JFormattedTextField ftf = (JFormattedTextField)jlayer.getView(); if (!ftf.isEditValid()) { //paint an error indicator using Graphics } }}//See http://download.oracle.com/javase/tutorial/uiswing/misc/jlayer.html for details
38. java.util.Objects Static utility methods for simplifying common operations on objects requireNonNull(T obj, String message)  Checks that the specified object reference is not null and throws a customized NullPointerException if it is int compare(T a, T b, Comparator<? super T> c)  Perform null and equals checks before invoking c.compare(a,b) boolean deepEquals(Object a, Object b) boolean equals(Object a, Object b)  Perform null checks and Arrays.deepEquals if necessary String toString(Object o, String nullDefault)  Perform null check and return nullDefault if o is null There are other similar methods, check javadoc
39. ThreadLocal  These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variableimport java.util.concurrent.atomic.AtomicInteger;public class UniqueThreadIdGenerator { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal<Integer> uniqueNum = new ThreadLocal<Integer> () { @Override protected Integer initialValue() { return uniqueId.getAndIncrement(); } }; public static int getCurrentThreadId() { return uniqueId.get(); }} // UniqueThreadIdGenerator
40. Locale Category Locale.setDefault(Category, Locale) Locale Locale.getDefault(Category) Category.DISPLAY  Category used to represent the default locale for displaying user interfaces Category.FORMAT  Category used to represent the default locale for formatting dates, numbers, and/or currencies What about default locale Locale.getDefault() ?  It stays as a different locale (not display or format)     But Locale.setDefault(Locale) sets 3 different locales now:display, format and default locales  Default locale depends on OS environment variables or value of –D flags. Set it to English for your health and change newly provided locales (display/format)
41. References Exploring JDK 7, Part 2: Translucent and Shaped Windows What’s New in NIO.2 (pdf) JDK 7 Features The Java NIO.2 File System in JDK 7 Xrender Proposal Java 2D Enhancements in Java SE 7 How to Decorate Components with the JLayer Class StackOverflow
New Features Of JDK 7 — Presentation Transcript
1. Java Reloaded JDK 7 by Deniz Oğuz
2. Objective Learn what is included in JDK 7 and remember to use Google when required
3. JDK Release History Version Release Date JDK 1.0 1996-01-23 JDK 1.1 1997-02-18 JDK 1.1.4 1997-09-12 JDK 1.1.5 1997-12-03 JDK 1.1.6 1998-04-24 JDK 1.1.7 1998-09-28 • Normal release cycle was 2 years, J2SE is 3 JDK 1.1.8 1999-04-08 J2SE 1.2 1998-12-04 years late. It should have bean released at the J2SE 1.2.1 1999-03-30 end of 2008. J2SE 1.2.2 1999-07-08 J2SE 1.3 2000-05-08 • Some of the most wanted features are J2SE 1.3.1 2001-05-17 postponed to J2SE 8. J2SE 1.4.0 2002-02-13 J2SE 1.4.1 2002-09-16 J2SE 1.4.2 2003-06-26 J2SE 5.0 2004-09-29 J2SE 6 2006-12-11 Future Releases J2SE 7 2011-07-28 J2SE 8 Expected in late 2012
4. How to Try JDK 7 Features?1. Download JDK 7 build from jdk7.java.net  (download zipped version and extract to a folder)2. Download Netbeans 7  (download SE version, 80 MB)3. In the IDE, choose Tools > Java Platforms from the main menu4. Click Add Platform and specify the directory that contains the JDK5. Ensure JDK 1.7 is chosen in the Platforms list and click Close6. On Project Properties Libraries section Select JDK 1.7 as Java     Platform7. On Project Properties Sources section Select JDK 7 as Source/Binary Format
5. New Javadoc Format
6. Project Coin String in switch statement Binary literals Underscore in literals Diamond operator Improved exception handling Try with resource Simplified vararg methods invocation
7. String in Switch Statement Before JDK 7 only byte, short, char, int were allowed in switch statements JDK 7 allows Strings to be used in switch statements Why isn’t long supported in switch statements? Console console = System.console(); String day= console.readLine(); switch (day) { case “monday” : console.writer().write(“1”);break; case “tuesday” : console.writer().write(“2”);break; case “wednesday” : console.writer().write(“3”);break; case “thursday” : console.writer().write(“4”);break; case “friday” : console.writer().write(“5”);break; case “saturday” : console.writer().write(“6”);break; case “sunday” : console.writer().write(“7”);break; default:console.writer().write(“?”); } console.flush();
8. Binary Literals It is now easier to specify numbers in binary form  int mask = 0b00000000000000000000000011111111;  int mask = Integer.parseInt(“00000000000000000000000011111111”, 2);  int mask = 255;
9. Underscores in Numbers int money = 100_000_000; long creditCNumber = 3434_3423_4343_4232L; int mask = 0b0000_0000_0000_0000_0000_0000_1111_1111;
10. Diamond Operator Before JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<Integer, Track>(); With JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<>();
11. Multi Catch and Final Rethrow Problem : You want to handle multiple exceptions with the same code block.try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException ex) {     log.warn(“Can not access file”, settingFile); } catch (FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException | FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }
12. Multi Catch and Final Re-throw Cont. Problem : You want to handle multiple exceptions with the same code block.public Setting readSettings(Strin settingFile) throws ParseException, IOException,FileNotFoundException { try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (Throwable ex) { log.warn(“Can not read settings”, settingFile); throw ex; } ……………..}
13. Try With Resourceprivate static String readConfiguration(String file) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } finally { if (reader != null) { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
14. Try With Resource Cont’private static String readConfigurationNew(String file) { try (BufferedReader reader = new BufferedReader(new FileReader(file));) { String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } }
15. Try With Resource and Autocloseable A new interface AutoCloseable is introduced Existing Closeable interface is changed to extend AutoCloseable interface A new method addSuppressed(Exception) is added to Throwable Exceptions throwed from close method of AutoCloseable are suppressed in favor of exceptions throwed from try-catch block See JavaDoc of Autocloseable for more detail
16. Autocloseable and Suppressed ExceptionExceptions from try-catch body suppresses exceptions thrown from AutoCloseable.close public ABCResource implements AutoCloseable { @Override public void close() throws ABCException { dosomething(); if (errorCondition) { throw new ABCException(“Not supported yet.”); } } } public ClientClass { public void useABCResource() { try (ABCResource resource = new ABCResource();) { dosomething(); if (errorCondition) { throw new ClientException(“Not supported yet.”); } } } }
17. Better Support for Other Languages in JVM  Statically Typed vs Dynamically Typed  Compile time type checking vs runtime type checking  Java, C, Scala are examples of statically typed languages  Java Script, Ruby are examples of dynamically typed languages  Strongly Typed vs Weakly Typed  Automatic type conversion as necessary vs fixed type  Java Script is weakly typed language  Java is a strongly typed languagepublic void printTotal(a, b) { print a + b;}public void printTotal(int a, int b) { print a + b;}
18. Dynamic Typing was Difficult to Implement on JVM• New bytecode is introduced • invokeinterface • invokestatic • invokevirtual • invokespecial • invokedynamic (Only bytecode in JVM that is not used by Java PL)• Execution environment of the programming language provides a bootstrapmethod for resolving method invocations
19. Fork/Join Framework Uses work-stealing algorithm Task is broken into smaller parts recursively A new ExecutorService implementation ForkJoinPool is added ForkJoinTask and its subclasses RecursiveAction and RecursiveTask are added
20. General Usage Pattern of Fork/JoinResult compute(Problem problem) { if (problem is small) directly solve problem else { split problem into independent parts fork new subtasks to solve each part join all subtasks compose result from subresults }else { RecursiveTask left = new ComputationTask(array, low, mid); RecursiveTask right = new ComputationTask(array, mid, high); right.fork(); return left.compute() + right.join();}main { RecursiveTask computationTask = new ComputationTask(array, 0, array.length); ForkJoinPool mainPool = new ForkJoinPool(); Long result = mainPool.invoke(computationTask);}
21. Work Stealing Less thread contention Improved data locality Execute large tasks early
22. TransferQueue Allows producers to wait until message is processed by a consumer even if the queue is not    full. transfer(E e)  Transfers the element to a consumer, waiting if necessary to do so. tryTransfer(E e)  Transfers the element to a waiting consumer immediately, if possible. tryTransfer(E e, long timeout, TimeUnit unit)  Transfers the element to a consumer if it is possible to do so before the timeout elapses. getWaitingConsumerCount()  Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timed poll hasWaitingConsumer()  Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take () or timed poll.
23. ThreadLocalRandom A random number generator isolated to current thread Aim is to reduce contention in multi threaded environments Usage:  ThreadLocalRandom.current().nextInt(min, max);  ThreadLocalRandom.current().nextLong(min, max);  ThreadLocalRandom.current().nextDouble(min, max);
24. ConcurrentLinkedDeque Unbound concurrent deque based on linked nodes Concurrent insertion, removal, and access operations execute safely across multiple threads Iterators are weakly consistent and do not throw ConcurrentModificationException size() method is NOT constant in time Bulk operations are not guaranteed to perform atomically
25. Phaser An reusable synchronization barrier Similar to CyclicBarrier or CountDownLatch but with more advance features :  Allows number of registered parties to change after Phaser creation  Each generation increment phase number of phaser  There are blocking and non blocking versions of operations Extremely flexible and complex, use Javadoc when you need to use this class
26. SCTP Support Feature SCTP TCP UDPConnection-oriented  Full duplex   Reliable data transfer  Partial-reliable data transfer optionalOrdered data delivery  Unordered data delivery  Flow control  Congestion control  ECN capable  Selective ACKs  optionalPreservation of message boundaries  Path MTU discovery  Application PDU fragmentation  Application PDU bundling  Multistreaming Multihoming Protection against SYN flooding attacks  n/aAllows half-closed connections  n/aReachability check  Psuedo-header for checksum (uses vtags)  Time wait state for vtags for 4-tuple n/a • Not all operating systems support SCTP
27. The Need for Java NIO.2 Methods works inconsistently  Delete method sometimes can not delete  Rename method sometimes can not rename No Exception is thrown from failed method Accessing metadata of files is limited Does not scale well  Listing a directory may take a long time (especially over network directories) A change notification facility is not provided Developers wanted to create their own file system implementations  For example an in-memory file system
28. Pluggable FileSystems java.nio.file.FileSystems is factory for file systems. Implement java.nio.file.spi.FileSystemProvider to provide your own file systems  A filesystem provider for Zip and Jar files are included in JDK 7  You can implement a filesystem to open a ISO image as a file system, or implement a RAM disk etc…… java.nio.file.FileSystems.getDefault() is used most of the time Multiple/Alternate views of same underlying files  Hides some files for security, read-only view, etc.
29. java.nio.file.Path and java.nio.file.Files Use java.nio.file.Path and java.nio.file.Files instead of java.io.File  Use java.io.File.toPath and java.nio.file.Path.toFile methods to integrate with legacy code java.nio.file.Paths contains factory methods for java.nio.file.Path  static Path getPath(String first, String… more)  static Path getPath(URI uri) Once you obtained Path object use java.nio.file.Files static methods to process  copy, createLink, createTempFile, delete, exist, getPosixFilePermissions, getAttribute, isHidden, isExecutable, isSymbolicLink, newByteChannel ……
30. Asynchronous I/O Allows application to continue on something while waiting for I/O Asynchronous I/O operations will usually take one of two forms:  Future<V> operation(…)  void operation(… A attachment, CompletionHandler<V,? super A> handler) See classes AsynchronousXXXXChannel that extends AsynchronousChannel Path path = Paths.get(“/home/deniz/dropbox/Getting Started.pdf”); AsynchronousFileChannel ch = AsynchronousFileChannel.open(path); ByteBuffer buf = ByteBuffer.allocate(1024); Future<Integer> result = ch.read(buf, 0); //read does not block while (!result.isDone()) { System.out.println(“lets do something else while waiting”); } System.out.println(“Bytes read = ” + result.get()); //Future.get will block ch.close();
31. Watch Service WatchService allows you to monitor a watchable object for changes and events. It implements Reactor pattern. Just like Selector does for Channels. Use Watchable.register to register with java.nio.file.WatchService to listen changes WatchKey register(WatchService watcher, WatchEvent.Kind<?>… events)  WatchEvent.Kind is an interface, actual applicable alternatives depends on Watchable Example Watchable objects  Path, FileSystem Path path = Paths.get(“C:/Users/Deniz/Dropbox/”); WatchService watcher = path.getFileSystem().newWatchService(); while (true) { path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); WatchKey key = watcher.take(); // block for event for (WatchEvent event : key.pollEvents()) { Path pathOfNewFile = (Path) event.context(); // path of new file System.out.println(“File is created:” + pathOfNewFile.toString()); } key.cancel(); }
32. FileVisitorPath start = …Files.walkFileTree(start, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE;   } public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } }});
33. FileTypeDetector Files.walkFileTree(Paths.get(“C:/Users/Deniz/Downloads”), new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileType = Files.probeContentType(file); System.out.println(file.toString() + “:” + fileType); return FileVisitResult.CONTINUE; } });Output:C:UsersDenizDownloadsWirofon-Client-0.2.10-rc01.exe:application/x-msdownloadC:UsersDenizDownloadsPlan B-She Said.mp3:audio/mpegC:UsersDenizDownloadsROM or Kernel-Update.pdf:application/pdf
34. Xrender Pipeline Will replace OpenGL pipeline on Unix, Linux platforms  Due to poor OpenGL drivers OpenGL pipeline has several problems  A lot of drivers has optimized Xrender implementations  Xrender is more suited 2D applications than OpenGL  Xrender applications are native X11 applications  Other GUI libraries are also using Xrender for 2D effects  QT4, GTK+, KDE4 Use -Dsun.java2d.xrender=true to enable it
35. Translucent Windows Java SE 6u10 introduced com.sun.awt.AWTUtilities to support translucent and shaped windows  AWTUtilities is removed in JDK7  Simple translucencysetUndecorated(true);setOpacity (opacityValue); //opacity value is between 0.0 – 1.0 Per-Pixel translucencyColor colorA = new Color (255, 0, 0, 0); // Transparent redColor colorB = new Color (255, 0, 0, 255); // Solid redsetBackground(new Color (0, 0, 0, 0)); // activate per-pixel  Transparent and shaped windows // translucency.protected void paintComponent (Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp = new GradientPaint ( 0.0f, 0.0f, colorA, 0.0f, getHeight (), colorB, true); g2d.setPaint (gp); g2d.fillRect (0, 0, getWidth (), getHeight ());}
36. Shaped Windows  Shaped windows  Only the parts that belong to the given Shape remain visible and clickable  A shaped window can also be translucent if you wantsetUndecorated(true);setShape(new Ellipse2D.Float (0, 0, getWidth (), getHeight ()));For code samples see: Exploring JDK 7, Part 2: Translucent and Shaped Windows
37. JLayer and LayerUI Similar to GlassPane but it is a layer around any JComponent not only JRootPane You can mask mouse events in installUI methodLayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();……JFormattedTextField dateField = new JFormattedTextField(dateFormat);JFormattedTextField numberField = new JFormattedTextField(numberFormat);……JPanel datePanel = new JPanel();……datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));datePanel.add(new JLayer<JFormattedTextField>(numberField , layerUI));……class ValidationLayerUI extends LayerUI<JFormattedTextField> { public void paint (Graphics g, JComponent c) { super.paint (g, c); JLayer jlayer = (JLayer) c; JFormattedTextField ftf = (JFormattedTextField)jlayer.getView(); if (!ftf.isEditValid()) { //paint an error indicator using Graphics } }}//See http://download.oracle.com/javase/tutorial/uiswing/misc/jlayer.html for details
38. java.util.Objects Static utility methods for simplifying common operations on objects requireNonNull(T obj, String message)  Checks that the specified object reference is not null and throws a customized NullPointerException if it is int compare(T a, T b, Comparator<? super T> c)  Perform null and equals checks before invoking c.compare(a,b) boolean deepEquals(Object a, Object b) boolean equals(Object a, Object b)  Perform null checks and Arrays.deepEquals if necessary String toString(Object o, String nullDefault)  Perform null check and return nullDefault if o is null There are other similar methods, check javadoc
39. ThreadLocal  These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variableimport java.util.concurrent.atomic.AtomicInteger;public class UniqueThreadIdGenerator { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal<Integer> uniqueNum = new ThreadLocal<Integer> () { @Override protected Integer initialValue() { return uniqueId.getAndIncrement(); } }; public static int getCurrentThreadId() { return uniqueId.get(); }} // UniqueThreadIdGenerator
40. Locale Category Locale.setDefault(Category, Locale) Locale Locale.getDefault(Category) Category.DISPLAY  Category used to represent the default locale for displaying user interfaces Category.FORMAT  Category used to represent the default locale for formatting dates, numbers, and/or currencies What about default locale Locale.getDefault() ?  It stays as a different locale (not display or format)     But Locale.setDefault(Locale) sets 3 different locales now:display, format and default locales  Default locale depends on OS environment variables or value of –D flags. Set it to English for your health and change newly provided locales (display/format)

41. References Exploring JDK 7, Part 2: Translucent and Shaped Windows What’s New in NIO.2 (pdf) JDK 7 Features The Java NIO.2 File System in JDK 7 Xrender Proposal Java 2D Enhancements in Java SE 7 How to Decorate Components with the JLayer Class StackOverflow

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)

Installing GoDaddy SSL Certificates on Glassfish v3 Step by Step

Introduction to Glassfish SSL

Newly created domains on glassfish has already a self signed certificate in DOMAIN_DIR/config/keystore.jks file. By default this keystore has default password of “changeit”. This certificate is named as s1as. To see this certificate you could issue following command:

keytool -list -keystore keystore.jks

When asked enter default password “changeit”. You will see a similar output to following:

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 6 entries

s1as, Dec 28, 2010, PrivateKeyEntry,
Certificate fingerprint (MD5): EA:56:23:46:7E:12:DA:6A:0D:8C:B9:12:11:0A:1A:8B

There should be a certificate with alias s1as. Since your glassfish will use this certificate by default, your domain.xml file in config folder of your domain will contain references to s1as from several places. We will change these references later.

I recommend you to change default password of keystore.jks. To change password use following command:

keytool  -sotrepasswd –keystore keystore.jks

When asked enter default password “changeit” and later enter new password for you keystore.jks. You should not forget this password. You will need this password for every operation you will perform on your keystore.jks file. Also you will be asked this password every time you start your domain. This password is called master password for your keystore.jks. Each entry in keystore.jks may have its own password, I recommend to make these passwords same with master password if possible.

There is another key file in glassfish’s domain folder named cacerts.jks. This file contains certificates from trusted authorities like godaddy, verisign etc. Certificates from this file is used to verify integrity of certificates you will purchase from certificate authorities. These certificates are called root certificates. But you may also import root certificates to keystore.jks too.

Purchasing Certificate and Installing Certificate

When you buy a certificate from a certificate authority (for example Godaddy) you have given a credit for certificate. You have to convert this credit to valid SSL certificate. To convert a credit to certificate you have to complete several step.

1) Check and correct Whois database entry for your domain:

Check your domain’s whois information and if they are not correct fix them. Especially your company name, and email address. GoDaddy will send an approval email to this email address.

2) Generate a certification request by using following steps:

Generate a new entry in keystore.jks with information of your domain.

keytool -keysize 2048 -genkey -alias www.yourdomain.com -keyalg RSA –dname "CN=www.domain.com,O=company,L=city,S=State,C=Country" -keystore keystore.jks

Enter password of you keystore when asked. GoDaddy requires at least 2048 bits keysize. CN is your sites domain name, O is your company name, L is the city, C is the 2 character country code. There are more options you could specify if you want. But these are enough. alias is the key you will use to refer this certificate. We will refer it from domain.xml.

Create the request file for submitting to Godaddy.

keytool –certreq –alias www.yourdomain.com –keystore keysore.jks –file cert_req.csr

Enter password of you keystore when asked.

cert_req.csr file will contains your certification request which you will submit to certificate authority. For GoDaddy you will open this file with a text editor and enter it to a text area as shown in following figure:

csr entry

You should include everything between and including followings.

—–BEGIN NEW CERTIFICATE REQUEST—–

—–END NEW CERTIFICATE REQUEST—–
After completing certification request submission. They will send an approval email to your email address shown on whois database.

3) Approve certification and import your certificates.

After approval you need to download a zip file which contains all certificates you need. During this step you will be asked for which server you are downloading certificates. You could select other because glassfish is not listed. Your download will contain 4 files:

  • gd_bundle.crt
  • gd_cross_intermediate.crt
  • gd_intermediate.crt
  • yourdomain.com.crt

First 3 of them are certificates belonging to godaddy.com. They are used to verify your domain’s certificate. They may already contained in your cacerts.jks but there is no harm importing them in your keystore.jks. Import these certificates to your keystore.jsk  using following steps:

keytool -import -alias root -keystore keystore.jks -trustcacerts -file gd_bundle.crt

keytool -import -alias cross -keystore keystore.jks -trustcacerts -file gd_cross_intermediate.crt

keytool -import -alias intermed -keystore keystore.jks -trustcacerts -file gd_intermed.crt

keytool -import -alias www.yourdomain.com -keystore keystore.jks -trustcacerts -file yourdomain.com.crt

If you are warned certificate already exist with a different alias choose yes to continue importing certificate.

As we have said at the beginning your domain’s domain.xml file contains references to s1as certificate. Open domain.xml with your editor of choice and replace every s1as with www.yourdomain.com and save it.

4) Test your setup

Start your domain using following command. You will be asked master password of your keystore.jks.

asadmin start-domain your_domain

Enter master password (3) attempt(s) remain)>Enter your master password here

Check your setup my navigating to https://www.yourdomain.com:ssl_port/. Your browser of choice will either warn you about invalid certificate, or you will see that it is verified by Godaddy.com. ssl_port is by default 8181, if you haven’t changed it yet from your domain.xml. If you change it to default 443, do not forget to configure your firewall to allow TCP over that port.

VN:F [1.9.22_1171]
Rating: 10.0/10 (2 votes cast)

java.lang.OutOfMemoryError and Native Memory

Thanks for the Memory” is a must read article for every java developer. It is a very good explanation of why sometimes you may need to REDUCE heap size to prevent java.lang.OutofMemoryError. You could download pdf of article writen by Andrew Hall and published at IBM Developerworks.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Counting Rows in Apache Cassandra

Update:As of today Apache Cassandra support Counter type and it should be used instead of this custom solution.

As you may already notice it is not possible to count number of rows in a column family.  In fact there is a request about that in Apache Cassandra bug database. Correctly doing this in a distributed database is a difficult thing. But a not so good but working solution could be implemented relatively easily. There are two alternative, first one removes row counting problem by changing structure of column families.

First of all you may even do not need to count number of rows, you may redesign your CF (column family)  and use columns to store information. Cassandra’s column names are different than column name of a relational database’s table. Unlike relational database’s every row may have different columns and number of columns for a row could be very large. Since Cassandra has a method that gives number of columns for a given row, counting columns instead of rows is a very easy operation provided that you could organize your CFs in that way.

But first solution may not be always applicable. Sometimes you really need to count number of rows.

Sample Problem:

Assume that we have a User column family and Post column family, and we want to display number of registered users and total number of posts for a specific user.

User:{
  username,
  fullName,
  posts:{
    timeUUID1:postID1,
    timeUUID2:postID2,
    timeUUID3:postID3,
    ...
    ... (add new colum as user adds new posts)
  }
}
Post:{
  postID,
  subject,
  dateTime,
  text
}

As you can see counting number of posts for each user is an easy operation, because Cassandra can easily return number of columns within a super column with get_count method. Just count number of columns within posts super column.

But getting total number of users is not easy unless you change your CFs. Lets implement a simple counter for our web application. Since our system is distributed, more than one application server may be accessing the database. We need to partition data for each server instance, each server will have a separate counter and will only increment it’s own value.  Each servers value will be kept as a new column in Cassandra. But while returning counter’s value. A server will add all values of servers. Since an application server is a multi-threaded environment we have to synchronize access to our increment method.

This solution has several disadvantages. First incrementing counter requires disk IO. Second, since increment operation  is synchronized only one thread may perform increment operation within a server.

1 @Singleton 2  public class Counter { 3 4 @Inject Logger logger; //log4j loggers ar thread safe 5   6 @Inject CassandraCounterDAO counterDAO; //this class is also thread safe 7 8 /** 9 * increment counter value and return new result. 10 * @param dataItem identifier of the counter to be incremented. 11 * @return new counter value. 12 */ 13 public synchronized long increment(String dataItem) { 14 long current = counterDAO.getCounter(dataItem); 15 current +=1; 16 counterDAO.setCounter(dataItem, current); 17 return current; 18 } 19 }

1 @Singleton 2 public class CassandraCounterDAO extends CassandraAbstractDAO { 3 4 public static final String COUNTER_DATA_SET = "Counter"; 5 private String hostname; 6 7 @PostConstruct 8 public void init() { 9 try { 10 hostname = InetAddress.getLocalHost().getHostName(); 11 } catch (UnknownHostException ex) { 12 logger.error("Can get hostname", ex); 13 throw new RuntimeException(ex); 14 } 15 } 16 17 /** 18 * Returns value of counter for given key. 19 * @param key identifier of counter. 20 * @return 21 */ 22 public long getCounter(String key) { 23 try { 24 Selector selector = Pelops.createSelector(MY_POOL, MY_KEYSPACE); 25 SlicePredicate columnSlicePredicate = Selector.newColumnsPredicateAll(false, Integer.MAX_VALUE); 26 List<Column> columns = selector.getColumnsFromRow(key, COUNTER_DATA_SET, columnSlicePredicate, ConsistencyLevel.QUORUM); 27 long total = 0; 28 for (Column column : columns) { 29 total += toLong(column); 30 } 31 return total; 32 } catch (Exception ex) { 33 logger.error("Can not query Counter", ex); 34 throw new RuntimeException(ex); 35 } 36 } 37 38 /** 39 * Increments value of counter and returns new value. 40 * @param key identifier ofcounter. 41 * @return 42 */ 43 public void setCounter(String key, long value) { 44 try { 45 Mutator mutator = Pelops.createMutator(MY_POOL, MY_KEYSPACE); 46 mutator.newColumn(hostname, toBytes(value)); 47 mutator.execute(ConsistencyLevel.ALL); 48 } catch (Exception ex) { 49 throw new RuntimeException(ex); 50 } 51 } 52 } 53

VN:F [1.9.22_1171]
Rating: 9.0/10 (2 votes cast)

Introduction to Real-Time Java


I wish I have more time to spend on this. But I am exhausted between my work,  my 3 months old child and this free time project.

I am not an expert on real-time Java. If you send your corrections to denizoguz@denizoguz.com, I will apply them to this presentation and upload it again.

Thanks.

You can download presentation in following formats.

Introduction to Real Time Java — Presentation Transcript

  1. Real Time Java Deniz Oğuz Initial:22.06.2008 Update:05.12.2009
    • Downside of Using C and Java Together
    • Aim of This Course
  2. Aim of This Course Gaining introductory level knowledge about Real-Time Java and evaluate its suitability to your next project.
  3. Outline
    • What is Real-time?
    • What are the problems of Java SE?
    • Real-Time Linux
    • Features of RTSJ (Real Time Specification for Java), JSR 1, JSR 282
    • IBM Websphere Real-Time
    • SUN RTS
  4. What is Real-time?
    • Real-time does not mean fast
      • Throughput and latency are important but does not enough
    • Real-time is about determinism
      • Deadlines must be met
    • Real-time is not just for embedded systems
      • Financial applications
      • Telecommunication applications
      • Network centric systems etc…
  5. Categories of Application Predictability Hard Real-Time None Real-Time Soft Real-Time No time-based deadlines Ex:Batch processing, web services Deadlines may be missed occasionally Ex:Router, automated trading systems Deadlines can not be missed Ex:fly-by-wire system, anti-lock brake system, motion control
  6. Hard/Soft real-time
    • Hard real-time
      • A late answer has no value
    • Soft real-time
      • late answer has still a value but value of answer rapidly degrees
  7. Software Complexity vs. Time Taken From IBM WebSphere Real Time: Providing predictable performance by Michael S. Fulton, Java chief architect;Darren V. Hart, Real Time team lead; andGregory A. Porpora, IBM Software Group. December 2006
  8. Latency and Jitter
    • Latency is the time between external event and system’s response to that event
    • Jitter is the variation in latency
  9. Hardware Architecture & OS
    • Modern processors and operating systems are optimized for throughput
      • It makes excellent sense for most systems to trade a rare factor of 100 times slowdown for a performance doubling everywhere else
  10. Hardware & OS Cont.
    • Worst-case scenario for an instruction
      • Instruction is not in the cache, processor must read it from memory
      • An address translation cache miss requires more memory access
      • Instruction might be in demand paged memory
      • Data may not be in cache
      • A large DMA may delay operations
      • SMI on X86 platforms
      • …….
  11. Worst Case Execution Cont. This example is taken from Real-Time Java Platform Programming by Peter C. Dibble 100101660 Total 10000000 One Big DMA 20000000 Demand paging for instruction read (write dirty page) 500 Dirty data cache write ATC miss 500 Instruction ATC miss 10 Execute Instruction 100000 Interrupts 10000000 Demand paging for data cache read (read page) 10000000 Demand paing for data cache write (read page) 20000000 Demand paging for data cache write (write dirty page) 500 Data cache read ATC miss 50 Instruction cache miss 20000000 Demand paging for data cache read (write page) 10000000 Demand paging for dirty data cache write (read page) 100 Data cache miss Estimate Time (ns) Event
  12. How to prevent worst case
    • Disable paging for time critical code
    • Use processor affinity and pin RT Threads to cpus
    • Use tunable DMA
    • Use large pages to reduce load on TLB
    • Disable interrupts or give RT Threads higher priority than some interrupts
    • On x86 architecture pay attention to SMI (System Management Interrupts)
      • Do not disable it completely , you may burn down your processor.
  13. Processor Pinning
    • Less context switching jitter
    • Decreased cache miss
    • Example (Linux and Sun RTS only):
      • -XX:RTSJBindRTTToProcessors=0,1
      • -XX:RTSJBindNHRTToProcessors=0,1
      • Alternatively you can create a cpu set named xx and use /dev/cpuset/xx
  14. Large Memory Pages
    • Garbage collectors performs very bad if memory is swaped to disk
    • For increased determinism use large memory pages and pin these pages to memory
    • Example Linux and Sun’s Java SE only:
      • echo shared_memory_in_bytes > /proc/sys/kernel/shmmax
      • echo number_of_large_pages > /proc/sys/vm/nr_hugepages
      • Start JVM using XX:+UseLargePages argument
      • Verify using cat /proc/meminfo | grep Huge

    Refer following Sun article for large memory pages: Java Support for Large Memory Pages

  15. RT-POSIX
    • An extension to POSIX standard to address hard and soft real-time systems (POSIX 1003.1b)
      • Priority Inversion Control and Priority Inheritance
      • New schedulers
      • Asynchronous IO
      • Periodic, Aperiodic and Sporadic Threads
      • High Resolution Timers
      • RT File System
      • Some others ….
    • For further information refer to RT Posix standard
  16. RT Linux
    • Fully preemptible kernel
    • Threaded interrupt handlers for reduced latency
    • High-resolution timers
    • Priority inheritance
    • Robust mutexes and rt-mutexes
    • To install use
    • sudo apt-get install linux-rt
    • in ubuntu. Select rt kernel at next system start-up
  17. Why Java?
    • Software (including embedded software) becomes more complex and gets unmanageable with old practices and tools
      • Ex:Financial systems, Network Centric systems
    • Single language, tools for real-time and non-real time parts
    • Java Platform provides a more productive environment
    • A large set of 3 rd party libraries are available
    • Has a very big community
      • Large number of developers
      • Support from big companies like IBM, SUN, Oracle
    • A lot safer when compared to low level languages
  18. Downside of Using C and Java Together
    • Same functionality is codded twice
      • for example coordinate conversion, distance calculations, R-tree etc. are implemented in C++ because they can not reuse PiriMap
    • High amount of integration problems
      • For example work package is divided between two people
    • Interfaces between C and Java is ugly and introduce overhead
      • For example DDS transfer Struct but large amount of code is written to convert these structs to Java classes and interfaces accustomed by java developers
    • Communication via JNI can violate safe features of Java
    • The JNI interface is inefficient
    • Increased maintenance cost in the feature due to above problems
  19. What are the problems of Java SE?
    • Dynamic Class Loading and Linking
    • JIT (Just in Time Compiler)
    • Thread Handling
    • Garbage Collector
    • No Raw Memory Access
    • No support for real-time operations, like deadline miss handling, periodic scheduling, processor pinning etc.
  20. Dynamic Class Loading
    • A Java-conformant JVM must delay loading a class until it’s first referenced by a program
      • Early loading is not allowed so JVM can not do this for you at application startup
    • This introduce unpredictable latency from a few microseconds to milliseconds.
      • Depends on from where classes are loaded
      • Static initialization
      • Number of classes loaded
  21. JIT (Just In Time Compiler)
    • Most modern JVMs initially interpret Java methods and, and later compile to native code.
    • For a hard RT application, the inability to predict when the compilation will occur introduces too much nondeterminism to make it possible to plan the application’s activities effectively.
  22. Garbage Collection
    • Pros
      • Pointer safety,
      • leak avoidance,
      • Fast memory allocation:faster than malloc and comparable to alloca
      • Possible de-fragmentation
    • Cons
      • Unpredictable pauses:Depends on size of the heap, number of live objects on the heap and garbage collection algorithm , number of cpus and their speed
  23. Main Garbage Collection Features
    • Stop-the-world or Concurrent
    • Moving objects
    • Generational
    • Parallel
  24. Garbage Collection in HotSpot
    • Serial Collector
      • -XX:+UseSerialGC
    • Parallel-scavenging
      • -XX:+UseParallelGC
    • Parallel compacting
      • -XX:+UseParallelOldGC
    • Concurrent Mark and Sweep (CMS)
      • -XX:+UseConcMarkSweepGC
  25. Garbage First Collector (G1)
    • Planned for JDK 7
    • Low pause and high throughput soft real-time collector
    • It is parallel and concurrent
    • Performs compaction
    • Devides heap to regions and further devides them to 512 bytes cards
    • It is not a hard real-time collector
  26. Thread Management
    • Although standard Java allows priority assignment to threads, does not require low priority threads to be scheduled before high priority ones
    • Asynchronously interrupted exceptions
      • Similar to Thread.stop but this version is safe
    • Priority Inversion may occur in standard Java/Operating systems
      • RTSJ requires Priority Inheritance
  27. What is RTSJ?
    • Designed to support both hard real-time and soft real-time applications
    • Spec is submitted jointly by IBM and Sun
    • Spec is first approved in 2002 (JSR 1)
    • Minor updates started on 2005 (JSR 282)
    • First requirements are developed by The National Institute of Standards and Technology (NIST) real-time Java requirements group
  28. Expert Group of RTSJ
  29. RTSJ’s Problem Domain
    • If price of processor or memory is a small part of whole system RTSJ is a good choice
      • If processor and memory price is very significant when compared with the rest of the system RTSJ may not be a good choice because you will need slightly more powerful processor and larger memory
      • For very low footprint applications with very limited resources you may consider a non RTSJ compliant virtual machine like Aonix Perc Pico or you may even consider a hardware JVM.
  30. Implementations
    • IBM Webshere Real Time (RTSJ Compliant)
      • Works on real-time linux kernel
      • There is a soft real-time version
    • SUN RTS (RTSJ Compliant)
      • Works on Solaris x86/Sparc or real-time linux kernel
    • Aonix Perc (Not RTSJ Compliant but close)
      • Perc-Ultra works on nearly all platforms
      • They have a safety critical JVM (Perc-Raven)
    • Apogee
      • Woks on nearly all platforms
  31. Main Features of RT Java Implementations
    • Full Java SE compatibility and Java syntax
    • A way to write programs that do not need garbage collection (New API)
    • High resolution timer (New API)
    • Thread priorities and locking (New API)
    • Asynchronous Event Handers (New API)
    • Direct memory access (New API)
    • Asynchronous Transfer of Control (New API)
    • AOT (A Head of Time) compilation (Not in RTSJ specification)
    • Garbage collection (Not in RTSJ specification)
  32. Real-Time Garbage Collectors
    • Time based (Metronome of Websphere RT)
    • Work based
    • Henriksson’s GC (Garbage Collector of Sun’s Java RTS is based on this)
  33. Sun’s RT Garbage Collector
    • 3 modes of execution
    • Non generational, concurrent and parallel collector
    • In normal mode works with priority higher than non-rt threads but lower than any rt thread.
    • When free memory goes below a certain threshold priority is increased to boosted priority but RT Threads (including the one with priority lower than GC) still continues to work concurently
    • When free memory goes below a critical threshold.All threads whose priority is lower than GC boosted priority are suspended
    • Most of its working can be tuned using command line switches like: NormalMinFreeBytes, RTGCNormalWorkers, RTGCBoostedPriority, BoostedMinFreeBytes etc.
  34. AOT, ITC and JIT
    • Nearly all Java SE vendors use JIT (Just in time compiler) due to performance reasons
    • Websphere RT uses AOT (Ahead of Time Compilation)
    • SUN’s Java RTS uses ITC (Initialization Time Compilation)

    Refer following IBM Article for further information: Real-time Java, Part 2: Comparing compilation techniques

  35. High-Resolution Timer
    • A more powerful notion of time
      • AbsoluteTime
      • RelativeTime
    • 84 bits value
      • 64 bits milliseconds and 32 bits nanoseconds
      • Range is 292 million years
    • Every RTSJ implementation should have provide at least one high resolution clock accessible with Clock.getRealTimeClock()
    • Resolution is system dependent
      • For example it is 200ns for SunFire v240 (Ref:Real-Time Java Programming by Greg Bollella)
      • Do not expect too much from a laptop
  36. HighResolutionTime Base Class
    • Base class for other 3 time classes.
    • Define the interface and provides implementation of some methods
    • This class and its subclass do not provide any synchronization
  37. AbsoluteTime and RelativeTime
    • AbsoluteTime represents a specific point in time
      • The string 03.12.2008 11:00:00.000 AM represents an absolute time
      • Relative to 00:00:00.000 01.01.1970 GMT
    • RelativeTime represents a time interval
      • Usually used to specify a period for a schedulable object
      • Can be positive, negative or zero
  38. Priority Scheduler
    • Normal scheduling algorithms try to be fair for task scheduling.
      • Even lower priority tasks are scheduled some times
    • There are different real-time and non real-time schedulers
      • Earliest-deadline first, least slack time, latest release time etc.
    • Fixed priority preemptive scheduler is most used real-time scheduler
  39. Thread Scheduling in RTSJ
    • Does not specify a scheduling implementation
    • Allow different schedulers to be plugged
    • Required base scheduler should be a priority scheduler with at least 28 unique priorities
    • Most implementations provide more than 28 priorities
    • More on this with Scheduling Parameters Slide
  40. What is Priority Inversion ?
    • Priority Inversion is the situation where a higher priority task waits for a low priority task.
    • There are 2 generally accepted solution
      • Priority Inheritance (required by RTSJ)
      • Priority Ceiling Emulation (optional for RTSJ)
  41. What is Priority Inversion? cont. Time Legend Lock request  Waiting Task T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 Med priority executing task High priority executing task Shared Resource         Low priority executing task
  42. Schedulable Interface
  43. Scheduling Parameters
  44. Different Task Types
    • Periodic
    • Aperiodic Tasks
    • Sporadic
  45. Release Parameters
  46. Sample Periodic Task public class HelloWorld extends RealtimeThread {        public static long[] times = (long[]) ImmortalMemory.instance().newArray(long.class, 100);           public HelloWorld(PeriodicParameters pp) {            super(null, pp);        }           public void run() {            for (int i = 0; i < 100; i++) {                times[i] = System.currentTimeMillis();                waitForNextPeriod(); //wait for next period            }        }           public static void main(String[] argv) {             //schedule real time thread at every 100 milisecond            PeriodicParameters pp = new PeriodicParameters(new RelativeTime(100, 0));            HelloWorld rtt = new HelloWorld(pp);            rtt.start();               //wait real time thread to terminate            try {                rtt.join();            } catch (InterruptedException ex) {                ex.printStackTrace();            }               //print results            for (int i = 0; i < 100; i++) {                System.out.println(times[i]);            }        }   }
  47. Periodic Execution vs Thread.sleep Real-time systems do not use a loop with a sleep in it to drive periodic executions While (true) { performPeriodicTask() Thread.sleep(period) } WRONG
  48. Deadline Miss!
    • For some applications a deadline should not be missed for any reason
    • For most applications dealine miss is bad, but it is not a total failure and a recovery is possible
    • In RTSJ there are 2 ways to detect a deadline miss
      • waitForNextPeriod() returns false immediately
      • Use a deadline miss handler which is an instance of AEH
  49. NoHeapRealTime Thread
    • Can not access heap
      • Can preempt GC immediately
      • If this rule is violated, MemoryAccessError is thrown
    • Can use ScopedMemory or ImmortalMemory
    • Should be created and started in a scoped or immortal memory
      • They can not be created from normal Threads
  50. WaitFreeWriteQueue
    • Intendent for exchanging data between real-time and non real-time part
    • Real-time producer does not block when queueing data
    • Multiple non-real time consumers may dequeue data
  51. WaitFreeReadQueue
    • Intendent for exchanging data between real-time and non real-time part
    • Real-time consumer does not block when read ing data
    • Multiple non-real time producers may queue data
  52. Memory Regions
    • Heap Memory
      • Same as in Java SE
      • Can be accessed with javax.realtime.HeapMemory
    • Scoped Memory
      • Created and sized at development time
      • Can be accessed with javax.realtime.ScopedMemory
      • Can not be garbage collected; reference count to ScopedMemory object is used. Finalize method of objects are called
      • Can be stacked
    • Immortal Memory
      • Can be accessed with javax.realtime.ImmortalMemory
      • Only one instance exist and size is determined at development time.
      • All static data and allocations performed from static initializers are allocated in Immortal memory. Interned Strings are also allocated in immortal memory
    • Physical Memory
      • There are LTPhysicalMemory, VTPhysicalMemory, and ImmortalPhysicalMemory
  53. Memory Regions
  54. Raw Memory Access
    • Models a range of physical memory as a fixed sequence of bytes
    • Allows device drivers to be writen in java
    • All raw memory access is treated as volatile, and serialized
  55. Raw Memory Access Cont private final long DEVICE_BASE_ADDRESS = xxxxxx ; private final long CTRLREG = 0; private final long STAT REG = 4 ; ……… p ublic void init() { RawMemoryAccess device = new RawMemoryAccess(type, DEVICE_BASE_ADDRESS); device.setInt(CTRL_REG, MY_COMMAND); //send command to device while (device.getInt(STATREG) != 0); //wait for device to response }
  56. Async Events
    • Many of the processing in RT systems are triggered by internal or external events
    • You don’t need to manage threads
    • Hundreds of AEHs may share a pool of threads
    • BoundAsyncEventHandler has always bounded to a dedicated thread
  57. Handling Posix Events
    • Use POSIXSignalHandler
      • Ex :

    ….. class SigintHander extends AsynchEventHandler { public SigintHandler() { //set it to highest priority setSchedulingParameters(new PriorityParameters(RTSJ_MAX_PRI); } public void handleAsynchEvent() { //handle user specified signal } } …… //add handler to posix predefined posix signal POSIXSignalHandler.addHandler(PosixSignalHandler.SIGUSR1, sigintHandler) Use kill -s SIGUSR1 PID to test

  58. Time Triggered Events
    • OneShotTimer : execute handleAsyncEvent method once at the specified time
    • PeriodicTimer : execute handleAsyncEvent method repeatedly at specified interval. A periodicTimer and a AEH combination is roughly equivalent to Periodic Threads.
    • Enable/Disable Timer : A disabled timer is still kicking. But it does not generate events. When enabled again, it continues like never disabled.
  59. Javolution Library (http://javolution.org)
    • High performance and time deterministic (util/lang/text/io/xml)
    • Struct and Union base classes for direct interfacing with native applications
    • NHRT Safe
    • Pure Java and less than 300Kbytes
    • BSD License
  60. IBM WebSphere Real Time
    • Runs on linux with real time patches applied to linux kernel on x86 platforms
    • Has AOT and JIT Compilers
    • Shared classes support
    • Contains Metronome: a real-time garbage collector
    • Full RTSJ 1.0.2 and Java SE 6.0 support
    • A well defined list of NHRT safe classes
  61. Metronome Garbage Collector
    • Uses a time based method for scheduling
    • Applications threads are given a minimum percentage of time (utilization)
      • User supplied at startup
    • Uses a new two-level object model for arrays called arraylets
  62. Metronome Garbage Collector Sample Execution
    • GC Pause Time Histogram
    • Utilization Graph
  63. Websphere AOT
  64. SUN RTS
    • Achieves maximum latencies of 15 microseconds, with around 5 microseconds of jitter.
    • Runs on real-time linux and Solaris 10
    • Has a real-time garbage collector
  65. IBM WebSphere Real Time
    • Runs on linux with real time patches applied to linux kernel
    • Has AOT and JIT Compilers
    • Contains Metronome real-time garbage collector
    • RTSJ and Java SE 5.0 Compliant
  66. Real World Projects:J-UCAS X-45C
  67. Real World Projects:FELIN
  68. Real World Projects:DDG-1000
  69. Real World Projects:ScanEagle
    • This milestone marked the first flight using the RTSJ on an UAV and received the Java 2005 Duke’s Choice Award for innovation in Java technology.
  70. Is RTSJ Required for You?
    • If your application can tolerate some degree of indeterminism use standard JVM and tune it to milliseconds level
    • Only if you fail the first approach use RTSJ.
      • Try to meet your timing constrains with real-time garbage collector (if available) without using advance/complex features like scoped memory
      • If first approach fails make use of NonHeapRealTimeThread, ImmortalMemory, ScopedMemory
  71. A comparison of the features of RTSJ with the increased predictability From IBM WebSphere Real Time Manual
  72. Safety Critical Java (JSR 302)
    • A subset of RTSJ that can be certified DO-178B and ED-128B.
    • Most probably garbage collector will not be available (not needed)
    • Will be targeted to Java ME platform, because Java SE and Java EE are too complex to be certified.
  73. Expert Group of JSR 302
    • Specification Lead: C. Douglass Locke (POSIX 1003.4 Real‑Time Extensions Working Group)
    • Expert Group
  74. Resources
    • Real-Time Java Platform Programming by Peter C. Dibble
    • Real-Time Java Programming with Java RTS by Greg Bollea
    • RTSJ Main Site ( www.rtsj.org )
    • IBM’s Developerworks Articles ( http:// www.ibm.com/developerworks/ )
    • SUN RTS ( http:// java.sun.com/javase/technologies/realtime/reference.jsp )
    • Deniz Oğuz’s blog ( www.denizoguz.com )
  75. Resources Cont. (JavaOne Presentations in 2008)
    • TS-4797 Fully Time-Deterministic Java Technology
      • Explains Javalution Library
    • TS-5767 Real-Time Specification for Java (JSR 1)
      • Explains status of RTSJ. Join presentation from SUN, IBM, Locke Consulting (JSR 302 spec lead)
    • TS-5925 A City-Driving Robotic Car Named Tommy Jr.
      • An autonomous ground vehicle fully powered by Java.
    • TS-5609 Real Time: Understanding the Trade-offs Between Determinism and Throughput
New Features Of JDK 7 — Presentation Transcript
1. Java Reloaded JDK 7 by Deniz Oğuz
2. Objective Learn what is included in JDK 7 and remember to use Google when required
3. JDK Release History Version Release Date JDK 1.0 1996-01-23 JDK 1.1 1997-02-18 JDK 1.1.4 1997-09-12 JDK 1.1.5 1997-12-03 JDK 1.1.6 1998-04-24 JDK 1.1.7 1998-09-28 • Normal release cycle was 2 years, J2SE is 3 JDK 1.1.8 1999-04-08 J2SE 1.2 1998-12-04 years late. It should have bean released at the J2SE 1.2.1 1999-03-30 end of 2008. J2SE 1.2.2 1999-07-08 J2SE 1.3 2000-05-08 • Some of the most wanted features are J2SE 1.3.1 2001-05-17 postponed to J2SE 8. J2SE 1.4.0 2002-02-13 J2SE 1.4.1 2002-09-16 J2SE 1.4.2 2003-06-26 J2SE 5.0 2004-09-29 J2SE 6 2006-12-11 Future Releases J2SE 7 2011-07-28 J2SE 8 Expected in late 2012
4. How to Try JDK 7 Features?1. Download JDK 7 build from jdk7.java.net  (download zipped version and extract to a folder)2. Download Netbeans 7  (download SE version, 80 MB)3. In the IDE, choose Tools > Java Platforms from the main menu4. Click Add Platform and specify the directory that contains the JDK5. Ensure JDK 1.7 is chosen in the Platforms list and click Close6. On Project Properties Libraries section Select JDK 1.7 as Java     Platform7. On Project Properties Sources section Select JDK 7 as Source/Binary Format
5. New Javadoc Format
6. Project Coin String in switch statement Binary literals Underscore in literals Diamond operator Improved exception handling Try with resource Simplified vararg methods invocation
7. String in Switch Statement Before JDK 7 only byte, short, char, int were allowed in switch statements JDK 7 allows Strings to be used in switch statements Why isn’t long supported in switch statements? Console console = System.console(); String day= console.readLine(); switch (day) { case “monday” : console.writer().write(“1”);break; case “tuesday” : console.writer().write(“2”);break; case “wednesday” : console.writer().write(“3”);break; case “thursday” : console.writer().write(“4”);break; case “friday” : console.writer().write(“5”);break; case “saturday” : console.writer().write(“6”);break; case “sunday” : console.writer().write(“7”);break; default:console.writer().write(“?”); } console.flush();
8. Binary Literals It is now easier to specify numbers in binary form  int mask = 0b00000000000000000000000011111111;  int mask = Integer.parseInt(“00000000000000000000000011111111”, 2);  int mask = 255;
9. Underscores in Numbers int money = 100_000_000; long creditCNumber = 3434_3423_4343_4232L; int mask = 0b0000_0000_0000_0000_0000_0000_1111_1111;
10. Diamond Operator Before JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<Integer, Track>(); With JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<>();
11. Multi Catch and Final Rethrow Problem : You want to handle multiple exceptions with the same code block.try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException ex) {     log.warn(“Can not access file”, settingFile); } catch (FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException | FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }
12. Multi Catch and Final Re-throw Cont. Problem : You want to handle multiple exceptions with the same code block.public Setting readSettings(Strin settingFile) throws ParseException, IOException,FileNotFoundException { try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (Throwable ex) { log.warn(“Can not read settings”, settingFile); throw ex; } ……………..}
13. Try With Resourceprivate static String readConfiguration(String file) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } finally { if (reader != null) { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
14. Try With Resource Cont’private static String readConfigurationNew(String file) { try (BufferedReader reader = new BufferedReader(new FileReader(file));) { String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } }
15. Try With Resource and Autocloseable A new interface AutoCloseable is introduced Existing Closeable interface is changed to extend AutoCloseable interface A new method addSuppressed(Exception) is added to Throwable Exceptions throwed from close method of AutoCloseable are suppressed in favor of exceptions throwed from try-catch block See JavaDoc of Autocloseable for more detail
16. Autocloseable and Suppressed ExceptionExceptions from try-catch body suppresses exceptions thrown from AutoCloseable.close public ABCResource implements AutoCloseable { @Override public void close() throws ABCException { dosomething(); if (errorCondition) { throw new ABCException(“Not supported yet.”); } } } public ClientClass { public void useABCResource() { try (ABCResource resource = new ABCResource();) { dosomething(); if (errorCondition) { throw new ClientException(“Not supported yet.”); } } } }
17. Better Support for Other Languages in JVM  Statically Typed vs Dynamically Typed  Compile time type checking vs runtime type checking  Java, C, Scala are examples of statically typed languages  Java Script, Ruby are examples of dynamically typed languages  Strongly Typed vs Weakly Typed  Automatic type conversion as necessary vs fixed type  Java Script is weakly typed language  Java is a strongly typed languagepublic void printTotal(a, b) { print a + b;}public void printTotal(int a, int b) { print a + b;}
18. Dynamic Typing was Difficult to Implement on JVM• New bytecode is introduced • invokeinterface • invokestatic • invokevirtual • invokespecial • invokedynamic (Only bytecode in JVM that is not used by Java PL)• Execution environment of the programming language provides a bootstrapmethod for resolving method invocations
19. Fork/Join Framework Uses work-stealing algorithm Task is broken into smaller parts recursively A new ExecutorService implementation ForkJoinPool is added ForkJoinTask and its subclasses RecursiveAction and RecursiveTask are added
20. General Usage Pattern of Fork/JoinResult compute(Problem problem) { if (problem is small) directly solve problem else { split problem into independent parts fork new subtasks to solve each part join all subtasks compose result from subresults }else { RecursiveTask left = new ComputationTask(array, low, mid); RecursiveTask right = new ComputationTask(array, mid, high); right.fork(); return left.compute() + right.join();}main { RecursiveTask computationTask = new ComputationTask(array, 0, array.length); ForkJoinPool mainPool = new ForkJoinPool(); Long result = mainPool.invoke(computationTask);}
21. Work Stealing Less thread contention Improved data locality Execute large tasks early
22. TransferQueue Allows producers to wait until message is processed by a consumer even if the queue is not    full. transfer(E e)  Transfers the element to a consumer, waiting if necessary to do so. tryTransfer(E e)  Transfers the element to a waiting consumer immediately, if possible. tryTransfer(E e, long timeout, TimeUnit unit)  Transfers the element to a consumer if it is possible to do so before the timeout elapses. getWaitingConsumerCount()  Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timed poll hasWaitingConsumer()  Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take () or timed poll.
23. ThreadLocalRandom A random number generator isolated to current thread Aim is to reduce contention in multi threaded environments Usage:  ThreadLocalRandom.current().nextInt(min, max);  ThreadLocalRandom.current().nextLong(min, max);  ThreadLocalRandom.current().nextDouble(min, max);
24. ConcurrentLinkedDeque Unbound concurrent deque based on linked nodes Concurrent insertion, removal, and access operations execute safely across multiple threads Iterators are weakly consistent and do not throw ConcurrentModificationException size() method is NOT constant in time Bulk operations are not guaranteed to perform atomically
25. Phaser An reusable synchronization barrier Similar to CyclicBarrier or CountDownLatch but with more advance features :  Allows number of registered parties to change after Phaser creation  Each generation increment phase number of phaser  There are blocking and non blocking versions of operations Extremely flexible and complex, use Javadoc when you need to use this class
26. SCTP Support Feature SCTP TCP UDPConnection-oriented  Full duplex   Reliable data transfer  Partial-reliable data transfer optionalOrdered data delivery  Unordered data delivery  Flow control  Congestion control  ECN capable  Selective ACKs  optionalPreservation of message boundaries  Path MTU discovery  Application PDU fragmentation  Application PDU bundling  Multistreaming Multihoming Protection against SYN flooding attacks  n/aAllows half-closed connections  n/aReachability check  Psuedo-header for checksum (uses vtags)  Time wait state for vtags for 4-tuple n/a • Not all operating systems support SCTP
27. The Need for Java NIO.2 Methods works inconsistently  Delete method sometimes can not delete  Rename method sometimes can not rename No Exception is thrown from failed method Accessing metadata of files is limited Does not scale well  Listing a directory may take a long time (especially over network directories) A change notification facility is not provided Developers wanted to create their own file system implementations  For example an in-memory file system
28. Pluggable FileSystems java.nio.file.FileSystems is factory for file systems. Implement java.nio.file.spi.FileSystemProvider to provide your own file systems  A filesystem provider for Zip and Jar files are included in JDK 7  You can implement a filesystem to open a ISO image as a file system, or implement a RAM disk etc…… java.nio.file.FileSystems.getDefault() is used most of the time Multiple/Alternate views of same underlying files  Hides some files for security, read-only view, etc.
29. java.nio.file.Path and java.nio.file.Files Use java.nio.file.Path and java.nio.file.Files instead of java.io.File  Use java.io.File.toPath and java.nio.file.Path.toFile methods to integrate with legacy code java.nio.file.Paths contains factory methods for java.nio.file.Path  static Path getPath(String first, String… more)  static Path getPath(URI uri) Once you obtained Path object use java.nio.file.Files static methods to process  copy, createLink, createTempFile, delete, exist, getPosixFilePermissions, getAttribute, isHidden, isExecutable, isSymbolicLink, newByteChannel ……
30. Asynchronous I/O Allows application to continue on something while waiting for I/O Asynchronous I/O operations will usually take one of two forms:  Future<V> operation(…)  void operation(… A attachment, CompletionHandler<V,? super A> handler) See classes AsynchronousXXXXChannel that extends AsynchronousChannel Path path = Paths.get(“/home/deniz/dropbox/Getting Started.pdf”); AsynchronousFileChannel ch = AsynchronousFileChannel.open(path); ByteBuffer buf = ByteBuffer.allocate(1024); Future<Integer> result = ch.read(buf, 0); //read does not block while (!result.isDone()) { System.out.println(“lets do something else while waiting”); } System.out.println(“Bytes read = ” + result.get()); //Future.get will block ch.close();
31. Watch Service WatchService allows you to monitor a watchable object for changes and events. It implements Reactor pattern. Just like Selector does for Channels. Use Watchable.register to register with java.nio.file.WatchService to listen changes WatchKey register(WatchService watcher, WatchEvent.Kind<?>… events)  WatchEvent.Kind is an interface, actual applicable alternatives depends on Watchable Example Watchable objects  Path, FileSystem Path path = Paths.get(“C:/Users/Deniz/Dropbox/”); WatchService watcher = path.getFileSystem().newWatchService(); while (true) { path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); WatchKey key = watcher.take(); // block for event for (WatchEvent event : key.pollEvents()) { Path pathOfNewFile = (Path) event.context(); // path of new file System.out.println(“File is created:” + pathOfNewFile.toString()); } key.cancel(); }
32. FileVisitorPath start = …Files.walkFileTree(start, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE;   } public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } }});
33. FileTypeDetector Files.walkFileTree(Paths.get(“C:/Users/Deniz/Downloads”), new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileType = Files.probeContentType(file); System.out.println(file.toString() + “:” + fileType); return FileVisitResult.CONTINUE; } });Output:C:UsersDenizDownloadsWirofon-Client-0.2.10-rc01.exe:application/x-msdownloadC:UsersDenizDownloadsPlan B-She Said.mp3:audio/mpegC:UsersDenizDownloadsROM or Kernel-Update.pdf:application/pdf
34. Xrender Pipeline Will replace OpenGL pipeline on Unix, Linux platforms  Due to poor OpenGL drivers OpenGL pipeline has several problems  A lot of drivers has optimized Xrender implementations  Xrender is more suited 2D applications than OpenGL  Xrender applications are native X11 applications  Other GUI libraries are also using Xrender for 2D effects  QT4, GTK+, KDE4 Use -Dsun.java2d.xrender=true to enable it
35. Translucent Windows Java SE 6u10 introduced com.sun.awt.AWTUtilities to support translucent and shaped windows  AWTUtilities is removed in JDK7  Simple translucencysetUndecorated(true);setOpacity (opacityValue); //opacity value is between 0.0 – 1.0 Per-Pixel translucencyColor colorA = new Color (255, 0, 0, 0); // Transparent redColor colorB = new Color (255, 0, 0, 255); // Solid redsetBackground(new Color (0, 0, 0, 0)); // activate per-pixel  Transparent and shaped windows // translucency.protected void paintComponent (Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp = new GradientPaint ( 0.0f, 0.0f, colorA, 0.0f, getHeight (), colorB, true); g2d.setPaint (gp); g2d.fillRect (0, 0, getWidth (), getHeight ());}
36. Shaped Windows  Shaped windows  Only the parts that belong to the given Shape remain visible and clickable  A shaped window can also be translucent if you wantsetUndecorated(true);setShape(new Ellipse2D.Float (0, 0, getWidth (), getHeight ()));For code samples see: Exploring JDK 7, Part 2: Translucent and Shaped Windows
37. JLayer and LayerUI Similar to GlassPane but it is a layer around any JComponent not only JRootPane You can mask mouse events in installUI methodLayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();……JFormattedTextField dateField = new JFormattedTextField(dateFormat);JFormattedTextField numberField = new JFormattedTextField(numberFormat);……JPanel datePanel = new JPanel();……datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));datePanel.add(new JLayer<JFormattedTextField>(numberField , layerUI));……class ValidationLayerUI extends LayerUI<JFormattedTextField> { public void paint (Graphics g, JComponent c) { super.paint (g, c); JLayer jlayer = (JLayer) c; JFormattedTextField ftf = (JFormattedTextField)jlayer.getView(); if (!ftf.isEditValid()) { //paint an error indicator using Graphics } }}//See http://download.oracle.com/javase/tutorial/uiswing/misc/jlayer.html for details
38. java.util.Objects Static utility methods for simplifying common operations on objects requireNonNull(T obj, String message)  Checks that the specified object reference is not null and throws a customized NullPointerException if it is int compare(T a, T b, Comparator<? super T> c)  Perform null and equals checks before invoking c.compare(a,b) boolean deepEquals(Object a, Object b) boolean equals(Object a, Object b)  Perform null checks and Arrays.deepEquals if necessary String toString(Object o, String nullDefault)  Perform null check and return nullDefault if o is null There are other similar methods, check javadoc
39. ThreadLocal  These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variableimport java.util.concurrent.atomic.AtomicInteger;public class UniqueThreadIdGenerator { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal<Integer> uniqueNum = new ThreadLocal<Integer> () { @Override protected Integer initialValue() { return uniqueId.getAndIncrement(); } }; public static int getCurrentThreadId() { return uniqueId.get(); }} // UniqueThreadIdGenerator
40. Locale Category Locale.setDefault(Category, Locale) Locale Locale.getDefault(Category) Category.DISPLAY  Category used to represent the default locale for displaying user interfaces Category.FORMAT  Category used to represent the default locale for formatting dates, numbers, and/or currencies What about default locale Locale.getDefault() ?  It stays as a different locale (not display or format)     But Locale.setDefault(Locale) sets 3 different locales now:display, format and default locales  Default locale depends on OS environment variables or value of –D flags. Set it to English for your health and change newly provided locales (display/format)
41. References Exploring JDK 7, Part 2: Translucent and Shaped Windows What’s New in NIO.2 (pdf) JDK 7 Features The Java NIO.2 File System in JDK 7 Xrender Proposal Java 2D Enhancements in Java SE 7 How to Decorate Components with the JLayer Class StackOverflow
VN:F [1.9.22_1171]
Rating: 9.0/10 (3 votes cast)

Real-Time Java Adaptation: Part 1

This article is the first of a 2 parts serial. We will try to evaluate suitablity of Real-Time Java for your applications and how you should adapt it with minimum effort.  Depending on a lot of factors, adaptation of real-time java can be very easy, especially your application does not have microseconds level of latency. Other than technical problems you may have to tackle social problems. Some of them come from C/C++ developers who perceive you as a threat to their career.  The most interesting criticism I have received was, “Real-time Java is not Java!” Since you may receive the same criticism, you should prepare your answer; “An RTSJ compliant implementation should pass all compatibility tests of Java SE”. So you can be sure that Real-time Java is Java and a bit more of it. Above criticism is made because RTSJ contains a lot of new APIs and mentions concepts a normal Java developer do not deal with. For example thread priorities and memory management. Especially javax.realtime.NonHeapRealTimeThread and existence of different memory regions adds a fair amount of complexity. But not all applications need all features of RTSJ. Adaptations of real-time java should be an incremental one.

You should evaluate your real-time requirements, your target hardware and operating system. Real-time Java will simplify your development and maintenance effort when compared to C/C++, but it will not be free. You have to trade some CPU and memory for this. If you look at Sun’s real-time implementation you will see its minimum hardware requirement is:

  • Dual core or dual CPU System
  • 512 Mbytes of RAM

So an RTSJ compliant implementation is not suitable for low-end hardware or systems in which CPU and memory costs are a significant percentage of total cost. For this kind of low end systems there are real-time java implementations but they are not real-time java specification compliant. For example Aonix has Perc Pico and Perc Raven products that are suitable for this kind of resource constrained systems.

If you pass above initial evaluation, I recommend you to investigate real-time requirements of your application further. Is it a hard real-time system or soft real-time system? If it is only a soft real-time system with high tolerance you may decide to stay with Java SE and tune garbage collector accordingly. Although garbage collection is not the only source of indeterminism, it has the greatest share. This solution will save you RTSJ licenses. You may try following alternatives:

  • G1 (GarbageFirst) Collector:

You may try follow New G1(Garbaga First) collector in Sun’s Java 6 is low-pause, low-latency soft real-time garbage collector. Much of its processing can be controlled through command line. For example; -XX:MaxGCPauseMillis=100 -XX:GCPauseIntervalMillis=1000 will try to achieve maximum pause time of 100ms within each 1000ms interval for application threads. Although it will try its best to achieve this goal it may cause longer pause times.

  • Old Serial Collector:

It is the most simple collector shipping with Sun’s JavaSE implementation. It stops all applications threads while performing collection. Depending on other factor, like processor speed, If your heap size is very small, this collector may be suitable.

  • Old CMS (Concurrent Mark Sweep) Collector:

This collector performs most of its work concurrently with application threads. Although it is not as good as G1 collector, depending on the other factors this collector is fairly good at keeping pause time of applications threads under 1 seconds. One drawback of this collector is it may cause memory fragmentation because it is not a compacting collector.

Garbage collection performance depends on dependent on the size of the heap, the amount of live data maintained by the application and the number and speed of available processors. Every application is different and you should invest some effort to tune garbage collector to increase throughput and reduce pause times. Unfortunately you generally have to trade throughput for low latency, not only in garbage collection, but also in most other areas of RT.

Not: All garbage collectors are seriously effected if your heap is swapped to disk by operating system. For this reason you should pin application’s memory pages to RAM and should use large memory pages for saving TLB (Translation Lookaside Buffer) entries. Your JVM and operating system should support this and most JVMs and operating systems do. Explaing how to configure large memory pages is out of the scope of this article. For Sun’ JavaSE implementation, I recommend following article: Java Support for Large Memory Pages

What if your latency requirements are not met by Java SE by only tuning your application and garbage collection? Then you may consider switching to a RTSJ compliant Java implementation. Since they are also certified as Java SE, porting your application may be very easy if you do not need hard real-time features. You should adopt RT specific features incrementally.

To be continued…

References:

  1. [Detlefs04] Detlefs, et. al., Garbage-First Garbage Collection, Sun Microsystems Research Laboratories, 2004.
  2. Dr. Dobb’s Article: G1: Java’s Garbage First Collector
  3. How to Configure Large Memory Pages for different operating systems? Java Support for Large Memory Pages
  4. Memory Mangement in HotSpot Virtual Machine. Download PDF.
VN:F [1.9.22_1171]
Rating: 9.0/10 (1 vote cast)

RTSJ Links You May be Interested

Updated on Nov 12, 2009.

  • Autonomous Audi TTS, Will Race to The Top of Pikes Peak in 2010 and Control Software is Developed with RT Java

More information at http://www.botjunkie.com/2009/10/24/stanfords-new-robotic-audi-tts-knows-how-to-drift-will-tackle-pikes-peak-next-year/


These articles are good starting for real-time Java (RTSJ)  if you are already a experienced Java  developer.

Technical documentation of Sun’s RTSJ implementation and various code examples.

A lots of articles about RTSJ, links to RTSJ Engineers blogs, presentations from various conferences including JavaOne.

  • Hear Metronome: The WebSphere Real Time GC

This video is published to youtube by IBM. It is a demonstration of the IBM’s WebSphere RealTime’s Metronome Garbage Collector. Same program is executed with and without real-time garbage collector. Effect of real-time garbage collector can be easily seen and heard.

  • A Helping Hand:Real-Time Java

An effective non-technical introduction to real-time systems and Real-Time Java.

  • An Industrial Robot Controlled by Real-Time Java

This robot arm drawing picture and playing classic knife game. Do you volunteer to put your hand under a knife controlled by this robot?

  • Application Design Using RTSJ by Doug Locke

A play list of 3 part presentation of Doug Locke about features of Real-Time Java Specification (RTSJ). It talks about what is real time, what are the advantages of Java compared to other languages and what RTSJ provides to enable real-time programming in Java.

  • An Introduction to Real-Time Java Specification (RTSJ) by Dr. Greg Bollella

A play list of 5 part presentation of former RTSJ specification lead Dr. Gred Bollella about features of Real-Time Java.

  • Java RTS Meets Wall Street by Eric Bruno

A play list of 2 part presentation that describes time critical operations in wall street (limit order etc.) and how real-time Java can be used to make more money improving responsiveness of application.

I have generated a play list of all RTSJ related videos on youtube. I will try my best to keep it up to date

Contains specification, list of books about Real-Time Java, links to lost of articles and some code samples. Also you can download reference implementation using the link in that site.

Several white papers about real-time systems and why you should real-time java in your next project. Aonix is one of the leading providers of real-time java platforms. They are also the only provider of a safety-critical java platform that can be used for DO-178B Level A. (Perc Raven).

Standard java libraries are not optimized for real-time world, for example standard ArrayList because it expands at runtime when needed. This implementation adds significant indeterminism. Javolution provides replacement libraries for text processing, io, collections etc. optimized for real-time applications.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Preparing for Real Time Java (RTSJ) Development

This post is moved from my old blog at http://denizoguz.blogspot.com. I have slightly modified it.

To start development with Real time java you need suitable virtual machine and a real time operating system that meets criterias required by your virtual machine.
Specification of real-time java is defined through JSR 1 and JSR 282 (small improvements). Followings are most known implementations of RTSJ.

  • Sun’s Java RTS : Current version is 2.2. It supports real-time Linux on x86 and Solaris 10 on x86 and sparc. It also supports 64 bits systems. Although previous versions did not have a real-time garbage collector (a more predictable garbage collector), latest versions has one. It supports a head of time compilation. These two features are not required by real time java specification. More information can be found at : http://java.sun.com/javase/technologies/realtime/
  • IBM’s Websphere Realtime : Current version is 2.0. It only works  on real-time Linux. It fully supports real-time java specification and contains a real-time garbage collector (metronome) and supports a head of time compilation.. More information can be found at : http://www.ibm.com/software/webservers/realtime/
  • Aonix’ PERC : Has different versions Ultra, Pico and Raven. It has also a real-time garbage collector, and supports a head of time compilation. More information can be found at : http://www.aonix.com/perc.html
  • Timesys’ RTSJ Reference Implementation : This is reference implementation and can not be used in production environment. Although you can use it four practice, I don’t recommend it, because you can download evaluation version of Sun RTSJ implementation. More information can be found at : http://www.timesys.com/java/

STEP 1 : Install Real Time Linux Kernel
I think the easiest way is to install Ubuntu and later download a real-time extensions by following the instructions at http://wiki.ubuntu.com/RealTime. It is very simple, just execute apt-get just like installing a normal application. After that reboot your machine and select real-time kernel at startup from grub menu.

STEP 2 : Download and Install a Real Time JVM
You can obtain a 90 day fully working evaluation version of Sun’ RTS 2.1 for Linux by registering with Sun’s developer network. Just fill the Sun Java Real-Time System Survey form and a download link will be mailed to you. Althoug it is not supported on ubuntu, as far as I see it is working.
You may use other versions but I could not recomend working with Timesys reference implementation because it based on a very old virtual machine and does not support new classes and feature after java version 1.3. Sun’s implemantation is fully compliant with JDK 5.0. Actually all RTSJ compliant implementations are required to pass test for JavaSE 5.0. That means you can run your JavaSE 5.0 applications as unmodified on a RTSJ compliant virtual machine by enjoying more determinism provided by real time garbage collector.

STEP 3: Prepare Eclipse
Open Window->Preferences and add Java RTS to Java->Installed JREs. Real time classes are in rt2.jar file in Sun’s implementation. Right click on this file in eclipse and add javadoc located in RTS_INSTALLATION_FOLDER/doc/rtsj-docs as javadoc for this jar file.

STEP 4 : HelloRealTime
Following program will schedule a real time thread at every 100 miliseconds and put current time in an array allocated from immortal memory. At the end prints content of this array.  By the way, you may need special privileges to access some real-time features, you can add your user to a special group or invoke JVM using sudo.

import javax.realtime.ImmortalMemory;
import javax.realtime.PeriodicParameters;
import javax.realtime.RealtimeThread;
import javax.realtime.RelativeTime;

public class HelloWorld extends RealtimeThread {
    //allocate long array from immortal memory
    public static long[] times = (long[]) ImmortalMemory.instance().newArray(long.class, 100);

    public HelloWorld(PeriodicParameters pp) {
        super(null, pp);
    }

    public void run() {
        for (int i = 0; i < 100; i++) {
            times[i] = System.currentTimeMillis();
            waitForNextPeriod(); //wait for next period
        }
    }

    public static void main(String[] argv) {
        //schedule real time thread at every 100 milisecond
        PeriodicParameters pp = new PeriodicParameters(new RelativeTime(100, 0));
        HelloWorld rtt = new HelloWorld(pp);
        rtt.start();

        //wait real time thread to terminate
        try {
            rtt.join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        //print results
        for (int i = 0; i < 100; i++) {
            System.out.println(times[i]);
        }
    }
}
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)