Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, January 2, 2013

Fibonacci Sequence With and Without Recursion


Fibonacci sequence is sequence with subsequent number is the sum of the previous two. See snapshot below from wikipedia.

We can implement this algorithm either using recursion or without. Let see both.Before the implementation let see how is the sequence defined for every F(n).

F(0) = 1 or F(0) = 0 in some definition
F(1) = 1
F(n) = F(n-1) + F(n-2)

Now these are implementation using Java

With Recursion
int fibo(int in){
 if(n <= 1){
  return n
 }else{
  return fibo(n-1) + fibo(n-2);
 }
}
Without Recursion
int fibo(int n){
 if(n <= 1){
  return n;
 }
 int fibo = 1;
 int fiboPrev = 1;
 for(int i = 2; i < n; ++i){
  int temp = fibo;
  fibo += fiboPrev;
  fiboPrev = temp;
 }
 return fibo;
}

Wednesday, July 27, 2011

java.lang.SecurityException When Shutdowning JBoss

We can run following command when starting JBoss. We assume there are already jboss system user in OS.


# Centos
su jboss -c '$JBOSS_HOME/bin/run.sh -b 0.0.0.0 > /dev/null 2> /dev/null &'
# Ubuntu
sudo -u jboss $JBOSS_HOME/bin/run.sh -b 0.0.0.0 > /dev/null 2> /dev/null &

Stopping jboss actually should be done by running this


# Centos
su jboss -c '$JBOSS_HOME/bin/shutdown.sh -S &'
# for ubuntu
sudo -u jboss '$JBOSS_HOME/bin/shutdown.sh -S &'

But in my system this will Exception in thread "main" java.lang.SecurityException: Failed to authenticate principal=null, securityDomain=jmx-console. This is happen because likely we have enabled the interceptor in jmx-invoker-service.xml file in deploy folder. Then for shuttingdown jboss we should provide some credential that we can see at login-config.xml file in the conf folder.


So the shutdown command should be like this
# Centos
su jboss -c '$JBOSS_HOME/bin/shutdown.sh -S -u admin -p admin &'
# for ubuntu
sudo -u jboss '$JBOSS_HOME/bin/shutdown.sh -S -u admin -p admin &'

Actually there still some exception when running this.


javax.management.InstanceNotFoundException: jboss.remoting:service=invoker,transport= bisocket,host=sudirman.nfsint.com,port=4457,JBM_clientMaxPoolSize=200,clientLeasePeriod=10000,clientSocketClass=org.jboss.jms.client.remoting.ClientSocketWrapper,dataType=jms,marshaller=org.jboss.jms.wireformat.JMSWireFormat,numberOfCallRetries=1,numberOfRetries=10,pingFrequency=214748364,pingWindowFactor=10,socket.check_connection=false,timeout=0,unmarshaller=org.jboss.jms.wireformat.JMSWireFormat is not registered.

and


2011-07-27 10:06:07,835 ERROR [org.jboss.remoting.transport.Connector] invalid Object Name
javax.management.InstanceNotFoundException: jboss.remoting:service=invoker,transport= socket,host=sudirman.nfsint.com,port=3873 is not registered.

But after running


ps ax | grep java

I cannot see any JBoss instance that still running. So I conclude that shutdowning has been done correcly. I will check the later issue when have time. Meanwhile those shutdowning command can be put into some script.


reference

Thursday, July 14, 2011

JBoss AS 7 has Released

It is little bit surprising when I browse to theserverside this evening and found a news that announce JBoss 7 has been released. I am surprised because what I remember JBoss 6 was released not too long a go. When I see the release date, JBoss 7 is released only 6 months after the JBoss 6.

JBoss application server is very important for me. Since my first step as Java Enterprise developer JBoss is the application server I have used most often. The funny thing, even though the version now already at 7 number, I myself still use the JBoss 4 version for development. This is happen because most of the project that I handled still use that version.

These are some important features of JBoss 7 Application server. Fast start up (< 3 seconds), lighweight, modular core, hot incremental deployment, elegant administration, domain management, first class component. I still don't have many comments for those features because I have to dirt my hand first playing the application. I will write another post later.

Tuesday, July 5, 2011

Detail Guide to Use JBoss Profiler

Long time I haven't write new post. Here the guide about remote profiling.

According to wikipedia, software profiling is a process of optimizing source code by analyzing the application running by profiler tools. By using profiler we can know how many times a method or function is invoked and also how long is taken for such invocation.

In the field Java, there so many profiler tools. Most known IDE usually have built in profiler tools. But there is sometime a need to do remote profiling. E.g profiling Java EE application that run in server. If we use JBoss application server then we are blessed by existence of JBoss profiler.

Excerpt from its website JBoss Profiler is a log based profiler using JVMPI and JVMTI. It uses an agent written in C that captures events from the JVM and logs to disk. A web application running on JBoss or another machine can be used to analyze these logs through a web browser.

The process of installing the profiler is very easy. First of course we need to download the newest version of the software from this link . In the time this post written I was using version 2.0 Beta5. Extract the files to some directory. I extract that to /opt directory. So the files will be available at /opt/jboss-profiler-<version>.

After extracting do the following steps exactly.
  1. Copy jboss-profiler.jar to jbossas/bin
  2. Copy jboss-profiler.properties to jbossas/bin
  3. Copy javassist.jar to jbossas/bin
  4. Edit jboss-profiler.properties in jbossas/bin to include the classes to be profiled
  5. Copy jboss-profiler-plugins.jar to jbossas/bin
  6. Edit run.conf (Unix) or run.bat (Windows) in jbossas/bin to include JBoss Profiler in JAVA_OPTS add the following argument: -javaagent:jboss-profiler.jar -Djboss-profiler.properties=jboss-profiler.properties
  7. Copy jboss-profiler.sar to jbossas/server/<conf>/deploy

Note: you don't need to do step number 3 if you use JBoss 5 or JBoss 6 application server.

After finished copying the needed files, open jboss-profiler.properties. The most important key is "includes". Add the java package you wish to profile. After it's done, we are ready to do profiling.

Start the server then run the following command from directory of our extracted jboss-profiler. In my case I extract to /opt/jboss-profiler-<version>. The command is

java -Xmx512m -Djboss-profiler-client.properties=jboss-profiler-client.properties -jar jboss-profiler-client.jar

After running the that there will be a list of availables commands as follow.

startProfiler : Start the profiler
stopProfiler : Stop the profiler
snapshot : Take a snapshot
getSnapshot : Get a snapshot
listSnapshots : List snapshots
clearSnapshots : Clear snapshots
gc : Trigger garbage collection
enable : Enable the profiler
disable : Disable the profiler
load : Load a snapshot
save : Save a snapshot
diff : Compare snapshots
add : Add classes (public|package|protected|private)
remove : Remove classes
list : List classes

The most important command is snapshot. Everytime we run this command the profiler client will produce profiler report in current directory. The report consist html files with detail of method invocation for every classes that we have included in configuration.

Some problem that might appear:
1. Forget copying jboss-profiler.sar to jbossas/server/<conf>/deploy will make jboss-proiler-client.jar won't run
2. Make sure to chmod every jar in /opt/jboss-profiler-<version> directory to be excecuted. Otherwise the profiler client also won't run.

Happy profiling and let's find the thing to optimize let's kill the memory leak.

Sunday, August 15, 2010

Java Must Have Book for Beginner

This post is pasted from my previous blog written 4 January 2008.

I am delving in learn Java programming language in this last 2 or three year previous. My interest in Java, perhaps is caused by my previous programming language I learn before, C language. We all know, although there is so much difference between them, Java inherit many fundamental syntax from C and C++.

Java is easy. Many people say that. Maybe it is true if we compare to the complicated C++. But, more than that, Java is not merely programming language. Java now is a complete huge framework that consist programming language itself, Application Programming Interface (API, a.k.a library that is in enormous amount), Java Virtual Machine which run byte code over operating system, etc. It seem clear now, learn Java demand our great focus and intention. Because it is not as easy as we imagine.

Thanks for Java’s book writer around the world and complete Java documentation and tutorial from Sun, there are always path for whom want to learn. Spare time for reading good stuff and continuous practice make Java acquaintance is really possible.

From many Java’s book around, I found several that is must have. First thing to read in learning this language is Java Tutorial from Sun website. But for the first read the tutorial is about too heavy. For experienced learner, tutorial is important reference in learning and developing code. Of course Java API’s reference also have to always exist in the directory for main reference.

The first book that I recommend is Java How to Program written by Deitel. Really complete book with enjoyable colored text that good enough against boring in reading. The books consist all of fundamental material to mid advance material such as basic of thread, GUI and servlet. Enormous code sample is spread around the book that can be tried for learning and doing approach. The next book is Advanced Java How to Program that is consisted advance material continuing the former book.

Although Java How to Program is enough for learning about syntax and the characteristic of Java API, there is no more explanation about build real software. I found two book that accomplish those necessity. Beginning Java Object by Jacquie Barker from Apress and Object-Oriented Analysis and Design written by Mike O’Dochery from Wiley. Barker book explain Java from software development point of view. In that book, reader is guided to build real Java software step by step. O’Docherty more focus in Object-Oriented approach mainly discuss about UML although it is really intended for reader who want develop Java application. Same as Barker, O’Docherty book also consist real software development as example.

Actually there are so many other book in Java Development theme. Some from them are easy to read while the other is little bit heavy. I hope four recommendation for Java Application Development Book will useful to you. Yup it is Java. And it is really phenomenon.

Saturday, August 14, 2010

Programming Language whose write JVM

This post is pasted from my previous blog posted 15 June 2008.

One of interesting point that is considered as the advantage of Java programming language is its portability. The jargon for that ability is really famous. Write once run anywhere (WORA). Although it will be needed hard work to write code which is really portable, I think the statement is not too exaggerate. Since it design phase, Java is prepared to be much portable. We can see the fact surround us. There are so many Java. In our computer desktop, laptop, expensive server, standard mobile phone, PDA, set top boxes and many more.

For who has read several first chapter of Java introductory book will know how that portability will be achieved. Different than former programming language, Java offer different approach in developing code. Running code in Java is consist of two phases which are, compiling source code and interpreting it’s result. Compiling phases use javac tool which change java source code with .java extension into intermediate code named bytecode with .class extension. Later this bytecode will be interpreted by java tool so we get the software run.

Different than other programming language, bytecode is not run straight above operating system or platform. Bytecode run over stuff called Java Virtual Machine (JVM). At that stuff .class file is interpreted a.k.a run by java.exe (in windoze) tool . Various platform has it’s own JVM. Windoze has its JVM, so that Linux, Solaris and other operating system. Also mobile phone, PDA, set top boxes has JVM. That’s why bytecode can run in various place and the portability is really approachable.

The question is, who act as a tittle of this post, what programming language has written that JVM. I believe rarely introductory java book will discuss much about inner mechanism of JVM. Several complex hardware level or compiler construction book perhaps will have wide portion for this topic. But I will prefer go to cinema and watch fun movie rather make my head dizzy reading such book.

So what the answer? This post will be useless by asking without answering. Ok here you go. After reading one of Good rarely Introductory Java book like I said above (the title is Java in 60 minutes A Day by Rich Rapossa from Willey) I found that JVM is written by C++. After all of advantage is offered by Java it still have to admit superiority of old complex difficult of C++. Yeah when reach the necessity of speed and hardware accessing such as needed by JVM, C++ is always needed.

My mind then imagine, if later there will be new programming language is founded. Let’s call that Sumatera programming language (Sumatera is another island in Indonesia such Java). Sumatera run above Virtual machine called Sumatera Virtual Machine (SVM). SVM abruptly coded by Java. And JVM coded with C++. I thing only crazy people will create that.

Just joke.