IIOP standalone EJB3 client lookup to Glassfish 3.1.2

May 18th, 2012

Setup to run a standalone EJB client and lookup to a Glassfish running EJB 3.1 stateless ejb.

  • Install Glassfish plugin in Eclipse.
  • Create a Enterprise Aplication Project using Glassfish server as the target runtime.
  • Create a HelloWorld example:
@Remote
public interface HelloWorld {}

@Stateless
public class HelloWorldEjb implements HelloWorld {}
  • Create a Application Client Project using Glassfish server as the target runtime.
  • Add gf-client.jar to the project classpath. gf-client.jar is included on the glassfish plugin folder inside eclipse, or in the glassfish standalone installation.
  • Create the plain java code as the following:
InitialContext ctx;
HelloWorld ejbRef = null;
try {
   ctx = new InitialContext();

   ejbRef = (HelloWorld) ctx.lookup("eu.pedrocardoso.HelloWorld");

   System.out.println(ejbRef.getMessage("Hello"));

} catch (NamingException e) {
   e.printStackTrace();
}

 

  • Optionally we can give properties to the initial context:
Properties p = new Properties();
p.put("org.omg.CORBA.ORBInitialHost","localhost");
p.put("org.omg.CORBA.ORBInitialPort","3700");

Default Glassfish iiop port is 3700

Related Posts:

  • No Related Posts

Want Better Estimates? Stop Estimating!

April 4th, 2012

Just to share a presentation about good estimation.

Video and slides: http://www.infoq.com/presentations/Want-Better-Estimates-Stop-Estimating

Related Posts:

  • No Related Posts

SCJP – practice results

July 14th, 2011

The results of my practice exams. Not included the mocks, questions that I did in several sites pointed by JavaRanch.

From OCP Java SE 6 Programmer Practice Exams book (more SCJP resources)

  • Assessment Test 1 - 71%
  • Assessment Test 1 - 29%
  • Exam 1 - 35%
  • Exam 2 - 50%
  • Exam 3 - 42%
  • Exam 4 - 58%
  • ExamLab Diagnostic Exam - 65%
  • Exam Lab Exam 1 - 59%
  • LearnKey Master Exam 1 - 65%
  • LearnKey Master Exam 1 - 70%
  • Oracle online practice exam - 87%
  • OCJP exam: 85% PASS

Related Posts:

scjp 1st june change to Pearson VUE

June 29th, 2011

Since 1st June the SCJP, now OCJP, the exam (Exam Number : 1Z0-851) have 60 questions, 150 minutes duration and you have to pass in 61% of the questions (37).

Seems that there are no drag and drop questions.

All answers here:

http://blogs.oracle.com/certification/entry/0596

From Oracle blog:

The exam objectives and the exam questions themselves did not change.

  • For three Java exams, some lengthy (time-consuming) questions were removed & replaced with shorter (less time-consuming) questions. This was done in order to shorten the required exam time (to 150 minutes).
  • Some interactive question types were removed from several Java and Solaris exams (including "matching" and "drag-and-drop" questions).

Related Posts:

SCJP – Java Enums

June 1st, 2011
  • enum CoffeeSize { BIG, HUGE, OVERWHELMING };
    • CoffeeSize cs = CoffeeSize.BIG;
  • enum CoffeeSize { BIG, HUGE, OVERWHELMING }; // <--semicolon  is optional here

 

  • CoffeeSize.values()  // returns a Collection of CoffeeSize enums. A Collection, not an array

 

  • Syntax example of a enum usage
public class WeatherTest {
   static Weather w;
   public static void main(String[] args) {
       System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
   }
}
enum Weather {
   RAINY, Sunny;
   int count = 0;
   Weather() {
      System.out.print("c ");
      count++;
   }
}
  • All of an enum’s values are initialized at the same time, and an enum’s variables are treated as if they were instance variables, not static variables

 

  • Enum declaration
    • Can declaring an enum outside a class
    • Can declaring an enum inside a class
    • Cannot declare enums in methods

 

  • enum really is a special kind of class
    • You can add constructors, instance variables, methods,
    • and something really strange known as a constant specific class body.
  • Constant specific class body: looks like an anonymous inner class for one enum value
    • OVERWHELMING(16) { body: methods override }

 

  • == or equals on enums values evaluates true in both cases.
  • The member variables declared inside an enum is by default static.

 

  • You can NEVER invoke an enum constructor directly
    • The enum constructor is invoked automatically. For example, BIG(8) invokes the CoffeeSize(int) constructor.
  • You can define more than one argument to the constructor, and
    • you can overload the enum constructors, just as you can overload a normal class constructor.

Related Posts:

SCJP – New Java 6 features

May 20th, 2011

Main classes and methods

  • nextLong()
    • Scans the next token of the input as a long. This method will throw InputMismatchException if the next token cannot be translated into a valid long value as described below. If the translation is successful, the scanner advances past the input that matched.
  • hasNextLong
    • Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. The scanner does not advance past any input.
  • findInLine(Pattern pattern)

Console

  •  System.console()
  • readLine(String format, String args) - returns a string with the line read.
  • Invoking close() on the objects returned by the reader() and the writer() will not close the underlying stream of those objects.
  • methods doesn't throw checked exceptions
  • console.reader().close();  //close method throws IOException
  • Invoking close() on the objects returned by the reader() and the writer() will not close the underlying stream of those objects.
  • Scanner.nextLong returns the next long, but doesn't advance to the the input if it's not a long.

NavigableSet / NavigableMap

  • It's a new interface (extends SortedSet / sorted Map) for Java 6.

Related Posts:

SCJP – Java I/O

May 5th, 2011

Some diagrams and tables in this post from SCJP Sun Certified Programmer for Java 6 book.

 

File The API says that the class File is "An abstract representation of file and directory pathnames." The File class isn't used to actually read or write data; it's used to work at a higher level, making new empty files, searching for files, deleting files, making directories, and working with paths.
FileReader This class is used to read character files. Its read() methods are fairly low-level, allowing you to read single characters, the whole stream of characters, or a fixed number of characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders, which improve performanceand provide more convenient ways to work with the data.
BufferedReader This class is used to make lower-level Reader classes like FileReader more efficient and easier to use. Compared to FileReaders, BufferedReaders read relatively large chunks of data from a file at once, and keep this data in a buffer. When you ask for the next character or line of data,it is retrieved from the buffer, which minimizes the number of times tha ttime-intensive, file read operations are performed. In addition, BufferedReader provides more convenient methods such as readLine(), thatallow you to get the next line of characters from a file.
FileInputStream A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
ObjectInputStream An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
FileWriter This class is used to write to character files. Its write()methods allow you to write character(s) or Strings to a file. FileWriters areusually wrapped by higher-level Writer objects such as BufferedWriters orPrintWriters, which provide better performance and higher-level, moreflexible methods to write data.
BufferedWriter This class is used to make lower-level classes likeFileWriters more efficient and easier to use. Compared to FileWriters,BufferedWriters write relatively large chunks of data to a file at once,minimizing the number of times that slow, file writing operations are performed. The BufferedWriter class also provides a newLine()method to create platform-specific line separators automatically.
PrintWriter This class has been enhanced significantly in Java 5. Because of newly created methods and constructors (like building a PrintWriter witha File or a String), you might find that you can use PrintWriter in placeswhere you previously needed a Writer to be wrapped with a FileWriter and/ora BufferedWriter. New methods like format(), printf(), and append()make PrintWriters very flexible and powerful.
Console This new, Java 6 convenience class provides methods to read input from the console and write formatted output to the console.
FileOutputStream A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
ObjectOutputStream An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconsituted on another host or in another process.

 

Don't forget to study the io objects constructors:

 

  • dir.list()  //returns a new String[] with a list of all file and folders in the dir directory;
  • new PrintWriter("myFile.txt");
    • When the constructor is called, it truncates the existing file to zero size.
  • File f1 = new File("sub1");;     f1.mkdir();
    • if the file exists doesn't throw a exception. It returns a boolean indicating whether or not a new
      directory was made
    • Most of interfaces of java.io returns boolean to indicate error or success.
  • File f3 = new File(f1, "sub3");;   PrintWriter pw = new PrintWriter(f3);
    • Creates a empty file, on the f1 folder, but only during the PrintWriter constructor.
  • f.renameTo() method takes only anothe File as arg, not a string.

Related Posts:

7 programming languages on the rise

April 21st, 2011

This is the title of an article on InfoWorld, that list the programming languages that are gaining more programmers.

  1. Python
  2. Ruby
  3. Matlab
  4. Javascript
  5. R
  6. Erlang
  7. Cobol
  8. CUDA extensions

The link to Google trends to compare those languages references on the web.

Related Posts:

  • No Related Posts

ThoughtWorks’ Fowler and Humble Talk Continuous Delivery

March 10th, 2011

Summary
Two of ThoughtWorks’ finest, Martin Fowler and Jez Humble, talk about the notion of Continuous Delivery, which enables organizations to build software that is production ready at all times. To do this, enterprises automate the build, deployment, and testing process, and improve collaboration between developers, testers, and operations. The duo discusses a variety of related issues.

Jez Humble is the product manager for the Go product at ThoughtWorks Studios and is co-author of “Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation.” Martin Fowler an author, speaker, and pundit on the topic of software development. He is chief scientist at ThoughtWorks and has written six books on software development.

Watch the interview here:

http://www.infoq.com/interviews/jez-humble-martin-fowler-cd

Related Posts:

  • No Related Posts

SCJP – Threads

February 28th, 2011

Dont forget

  • wait, notify or notifyAll must have the this object lock. So look if they are in a syncrhonized method

 

 

 

  • To analyze synchronized method issues, don't forget to verify if the threads are being created in same runnable object. If don't the synchronized methods locks will be distinct.
  • public void run() - is the signature of the method that must be extended to implement Runnable or to extend Thread
  • JVM exit after all the non demons threads created by the application complete, or when a thread executes System.exit().
    • if a main thread exits or if it throws a runtime exception, the child threads proceeds without any problem.
defined inThread defined inObject static final declare achecked exception Can throw aruntime exception
run X
start X
join X X
sleep X X
yield X X
currentThread X X
wait X X
notify X
notifyAll X

 

  • isAlive() - tests if this thread is alive. A thread is alive if it has been started and has not yet died.
  • If the thread doesn't own a lock on the object, and if we try to wait on this object, an IllegalMonitorStateException is thrown at runtime.
  • If a thread is waiting on another thread, and if those thread finishes the main thread is released without any problem.
  • synchronized block should be defined on an object: synchronized(this) {}
  • Thread.sleep causes the currently executing thread to sleep, even if called on a thread instance, not on the thread refereed in the code
  • Priority 1 is low priority, 5 is normal, 10 is maximum prioprity
  • interrupt and isInterrupted are Thread methods

 

  • The small execution block (means: don't allow to change running thread) - is per bytecode line, not by intermediate expression. To be confirmed
  • to a thread call wait, notify or notifyAll on a obj, the thread must own the lock for those obj.
  • Thread.sleep - don't release the lock
  • wait releases the lock

 

Synchronized  methods

  • public static SafeDeposit getInstance(int code) {} , with a private static instance and a private constructor
    • does not guarantees only one instance, because the getinstance is not synchronized

 

  • A class to be thread safe, the variables need to be private, and the get and set methods need to be synchronized

Related Posts: