Friday, June 21, 2013

How to solve "javax.naming.CommunicationException [Root exception is java.net.ConnectException" thrown when connecting to a EJB in weblogic server

Recently during a test done on an EJB, I got the following exception.

javax.naming.
CommunicationException [Root exception is java.net.ConnectException: t3://server01:7101: Bootstrap to server01/10.10.58.26:7101 failed. It is likely that the remote side declared peer gone on this JVM]
javax.naming.
CommunicationException [Root exception is java.net.ConnectException: t3://server01:7101: Bootstrap to server01/10.10.58.26:7101 failed. It is likely that the remote side declared peer gone on this JVM]
    at weblogic.jndi.internal.
ExceptionTranslator.toNamingException(ExceptionTranslator.java:40)
   ......

Caused by: java.net.ConnectException: t3://
server01:7101: Bootstrap to server01/10.10.58.26:7101 failed. It is likely that the remote side declared peer gone on this JVM
    at weblogic.rjvm.RJVMFinder.
findOrCreateInternal(RJVMFinder.java:216)
..............

    ... 25 more
Caused by: java.rmi.ConnectException: Bootstrap to
server01/10.10.58.26:7101 failed. It is likely that the remote side declared peer gone on this JVM
    at weblogic.rjvm.
ConnectionManager.bootstrap(ConnectionManager.java:334)
    at weblogic.rjvm.RJVMManager.
findOrCreateRemoteInternal(RJVMManager.java:254)
    at weblogic.rjvm.RJVMManager.
findOrCreate(RJVMManager.java:197)
    at weblogic.rjvm.RJVMFinder.
findOrCreateRemoteServer(RJVMFinder.java:238)
    at weblogic.rjvm.RJVMFinder.
findOrCreateInternal(RJVMFinder.java:200)
    ... 31 more

 It turned out that the reason for this exception is, the listen address (lets say "lstnr01") declared in the managed server in Weblogic Admin console is different from server name (although it is a correct address to that machine, I can use it to connect to weblogic console and do ssh) I used for initiating the connection to EJB. You can check the listen address of the server by browsing to "Environments"->Servers->YourServer->Configuration->General. In that page, there is a text box to enter "Listen Address".

Now after finding the reason for the problem, next problem I got is I cannot use this listen address "lstnr01" to connect to my machine, because that is not registered in a DNS server in the network. The mapping of "lstnr01" to that machine was only configured in the "/etc/hosts" file of the server.
Therefore to fix that issue, I added the same IP host name mapping entry to map "lstnr01" to server IP in the C:/Windows/system32/drivers/etc/hosts/hosts file.

10.10.58.26  lstnr01  

Then I executed the "ipconfig /flushdns" command using the command line. It refreshed the DNS cache of the local machine and updated the cache with newly added entry in the hosts file.
Then I used "lstnr01" as the remote server address instead of "server01" when connecting to the EJB.
 Then the above exception was gone and I was able to connect to the EJB and run the EJB method successfully.

How to highlight a searching text in "tail -f" output

When we are investigating an software issue of a software run on a unix machine, we normally use "tail -f" command to see the live log messages logged by the software (for e.g: checking managed server logs in a weblogic server). If you are looking the log for finding a specific log message, it will be easier if you can make the search word highlighted in some color in the output.

Following method is a one of way you can achieve that task. Please note that to write ^[ character, you should press Ctrl+V and then Ctrl+[

tail -f testEcho.txt | sed "s/\\(search\\)/^[[33;1m\\1^[[0m/g"

 



Wednesday, June 12, 2013

Writing a custom JUnit runner for running a single method using command line

Recently I needed to create a custom JUnit runner for executing a single method using the command line.  This is how I implemented that.


package testpackage;

import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;

public class SingleJUnitTestRunner {

    /**
     * Custom built junit runner for running single method.
     * Usage:
     * java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod
     */
    public static void main(String... args) throws ClassNotFoundException {
        if (args.length == 0){
            System.out.println("Usage:");
            System.out.println("java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod");
            System.exit(-1);
        }
        String[] classAndMethod = args[0].split("#");

        if (classAndMethod .length != 0){
            System.out.println("Usage:");
            System.out.println("java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod");
                     System.exit(-1);
        }
        System.out.println("======================================");
        System.out.println("Test Execution started for " + classAndMethod[1] + " method in class " + classAndMethod[0] + ".");
        System.out.println("=====================================");

        Request request = Request.method(Class.forName(classAndMethod[0]),
                classAndMethod[1]);
        JUnitCore core = new JUnitCore();
        // Use the text listener to get the output from test cases to console
        RunListener listener= new TextListener(System.out);
        core.addListener(listener);
        Result result = core.run(request);


        System.out.println("RunCount: " + result.getRunCount());

        System.out.println("FailureCount: " + result.getFailureCount());
        System.out.println("IgnoreCount: " + result.getIgnoreCount());
        System.exit(result.wasSuccessful() ? 0 : -1);
    }

}