Wednesday, January 6, 2010

Part 2: NoRouteToHostException

Part1 : Part2

If following code run without proxy details, then java.net.NoRouteToHostException will be thrown in windows/linux platform


import java.net.Socket;
public class SocketPrg {
public static void main(String[] args) throws Exception{
Socket sock=new Socket("abc.javafundu.com",8001);
}
}


UnknownHostException

Assume, I have given proper proxy details if internet connection established through proxy server. If we refer wrong host name which does not exsit then we do get java.net.UnknownHostException.

Additionally, we may refer right host name then also we do get this exception. In this case we have to check hosts file and correct the entry.

windows: /windows/system32/drivers/hosts
Linux : /etc/hosts

Make following entry in hosts file
127.87.6.207 abc.javafunud.com javafundu.com
Exception will be thrown as below


Exception in thread "main" java.net.UnknownHostException: abc.javafundu.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:7)


ConnectException

If host name is correct and hosts points right IP address with wrong port number then ConnectException will be thrown with Connection timed out for remote host most of the time( with default timeout configuration). For localhost or same network host, we do get Connection refused.

Socket sock=new Socket("localhost",8001);


Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:7)

Socket sock=new Socket("javafundu.com",8001);


Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:9)

Tuesday, January 5, 2010

NoRouteToHostException

java.net.NoRouteToHostException thrown when tried to access remote host and port, where firewall blocks the access. In general, -Dhttp.proxyHost and -Dhttp.proxyPort is incorrect then we do get this exception in Linux environment.


import java.io.InputStream;
import java.net.URL;
public class SocketPrg {
 public static void main(String[] args) throws Exception{
  URL url =new URL("http://schemas.xmlsoap.org/soap/envelope/");
  InputStream is=url.openStream();
 }
}

javac SocketPrg.java

java SocketPrg

In Linux, the following exception will be thrown

Exception in thread "main" java.net.NoRouteToHostException: No route to host
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:852)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:793)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:718)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1041)
at java.net.URL.openStream(URL.java:1009)
at SocketPrg.main(SocketPrg.java:16)

In Windows, the following exception will be thrown for the above program

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at SocketPrg.main(SocketPrg.java:16)

Run the above, program with commandline arguments with proper proxy settings

Solution:
java -Dhttp.proxyHost=www-proxy.us.abc.com -Dhttp.proxyPort=80 SocketPrg

Wednesday, December 30, 2009

MBean Descriptors

JMX MBean/MXBean get accessed by different clients(Ex: JConsole, VisualVM, and etc.,), where it is our responsibility that to explain/descirbe the details of the Bean, it includes

  • MBean author, version and etc.,
  • Attribute display name, type, minValue, maxValue, defaultValue and recommendedValue and its behaviour
  • Operation display name, and its argument details

Java SE 6 has come up integrated way of annotation approach for all types of MBean, where we can specify the additional details, is called Descriptors.

Author.java
package com.example.mxbeans; 
 
import java.lang.annotation.Documented; 
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
import javax.management.DescriptorKey; 
 
@Documented 
@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface Author { 
    @DescriptorKey("Author") 
    String value(); 
} 

HelloWorldMXBean.java

package com.example.mxbeans; 
 
@Author("Krishna")
public interface HelloWorldMXBean { 
    @DisplayName("GETTER: Name") 
    public QueueSample getName(); 
    @DisplayName("OPERATION: clearHelloCount") 
    public void clearCount(); 
} 

In runtime these metadata/annotation converts in to JAVA object, for most constructors in the classes MBean*Info (MBeanInfo, MBeanAttributeInfo, and so on), a parallel constructor exists with the same parameters plus an additional javax.management.Descriptor parameter. The same is true for OpenMBean*InfoSupport. The MBean*Info and OpenMBean*InfoSupport classes contain a getDescriptor() method.

Read: MBean Descriptors Tutorial

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com