I have a implement Database TNS Ping tester using ADF View Controller and Java backing bean. It is just required IP Address and Port of your database. I used JDeveloper 11.2.3.0 and only ViewController project and very small project.
This is my backing bean class. It connected with my JSPX:
package mn.view;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
/**
 * @author Erdenebayar.E
 * @link erdenebayare.blogspot.com
 */
public class TNSPingTester {
    static String CONN_SUCCESSFULLY = "Connection is healthy";
    static String CONN_UNSUCCESSFULLY = "Please check your connection";
    String ipAddress = "192.168.100.5";
    Integer dbPort = 1521;
    String finalResult = "";
    public String checkDBConnection() {
        //pass in a byte array with the ipv4 address, the port & the max time out required;
        String[] ipAddresses = getIpAddress().split("\\.");
        long start = -1; //default check value
        long end = -1; //default check value
        long total = -1; // default for bad connection
        byte[] addr1 = new byte[] { };
        try {
            addr1 =
                    new byte[] { (byte)Integer.parseInt((ipAddresses[0])), (byte)Integer.parseInt((ipAddresses[1])), (byte)Integer.parseInt((ipAddresses[2])),
                                 (byte)Integer.parseInt((ipAddresses[3])) };
        } catch (Exception ex) {
            setFinalResult(ex.getMessage());
            return getFinalResult();
        }
        
        int timeoutMs = 5000; // 5 seconds
        //make an unbound socket
        Socket theSock = new Socket();
        try {
            InetAddress addr = InetAddress.getByAddress(addr1);
            SocketAddress sockaddr = new InetSocketAddress(addr, getDbPort());
            // Create the socket with a timeout
            //when a timeout occurs, we will get timout exp.
            //also time our connection this gets very close to the real time
            start = System.currentTimeMillis();
            theSock.connect(sockaddr, timeoutMs);
            end = System.currentTimeMillis();
        } catch (UnknownHostException e) {
            start = -1;
            end = -1;
        } catch (SocketTimeoutException e) {
            start = -1;
            end = -1;
        } catch (IOException e) {
            start = -1;
            end = -1;
        } finally {
            if (theSock != null) {
                try {
                    theSock.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if ((start != -1) && (end != -1)) {
                total = end - start;
            }
        }
        if (total == -1) {
            setFinalResult(CONN_UNSUCCESSFULLY);
        } else {
            setFinalResult(CONN_SUCCESSFULLY);
        }
        System.out.println(getFinalResult());
        return getFinalResult();
    }
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }
    public String getIpAddress() {
        return ipAddress;
    }
    public void setDbPort(Integer dbPort) {
        this.dbPort = dbPort;
    }
    public Integer getDbPort() {
        return dbPort;
    }
    public void setFinalResult(String finalResult) {
        this.finalResult = finalResult;
    }
    public String getFinalResult() {
        return finalResult;
    }
}
I have a attached final result of web result.
Connection is healthy.

Connection is unhealthy.
 
If you want source code: Source Code TNS Ping Tester.
Connection is healthy.
Connection is unhealthy.
If you want source code: Source Code TNS Ping Tester.
Thank you,
Erdenebayar
Linkedin: http://www.linkedin.com/in/erdenebayare
Twitter: http://twitter.com/#!/erdenebayare
Erdenebayar
Linkedin: http://www.linkedin.com/in/erdenebayare
Twitter: http://twitter.com/#!/erdenebayare
No comments:
Post a Comment