Posts tagged Java
Best eclipse shortcut
0I 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?
New Features of JDK 7 Explained
0For 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.
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
Installing GoDaddy SSL Certificates on Glassfish v3 Step by Step
1Introduction 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:
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.
java.lang.OutOfMemoryError and Native Memory
0“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.
Counting Rows in Apache Cassandra
2Update: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
Real-Time Java Adaptation: Part 1
1This 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:
- [Detlefs04] Detlefs, et. al., Garbage-First Garbage Collection, Sun Microsystems Research Laboratories, 2004.
- Dr. Dobb’s Article: G1: Java’s Garbage First Collector
- How to Configure Large Memory Pages for different operating systems? Java Support for Large Memory Pages
- Memory Mangement in HotSpot Virtual Machine. Download PDF.
RTSJ Links You May be Interested
0Updated 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.
Preparing for Real Time Java (RTSJ) Development
0This 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]);
}
}
}