com.app.services.ExecutorServiceThread.java Source code

Java tutorial

Introduction

Here is the source code for com.app.services.ExecutorServiceThread.java

Source

package com.app.services;

/*Copyright 2013 - 2015, Arun_Soundararajan (arun_srajan_2007@yahoo.com).and/or its affiliates.
    
All files in this repository or distribution are licensed under the
Apache License, Version 2.0 (the "License");
you may not use any files in this repository or distribution except
in compliance with the License.
    
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.commons.vfs2.impl.VFSClassLoader;
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.binder.DigesterLoader;
import org.apache.commons.io.input.ClassLoaderObjectInputStream;
import org.apache.log4j.Logger;
import org.xml.sax.InputSource;

import com.app.server.ServerConfig;
import com.app.server.WebClassLoader;
import com.app.server.node.CustomServerSocket;
import com.app.server.node.NodeInfo;
import com.app.server.node.NodeInfoMethodParam;
import com.app.server.node.NodeResourceInfo;
import com.app.server.node.SerializableSocket;

/**
 * This class is the implementation of the Executor service Thread
 * 
 * @author arun
 * 
 */
public class ExecutorServiceThread implements Runnable, ExecutorServiceThreadMBean {
    ServerSocketChannel serverSocketChannel;
    Hashtable executorServiceMap;
    int shutDownPort;
    Hashtable ataMap;
    Hashtable urlClassLoaderMap;
    String deployDirectory;
    public int numberOfServicesRequests = 0;
    String[] nodesport;
    String warDeployerName;
    String isdeployer;
    ObjectName objName;
    ScheduledExecutorService schexc;
    Hashtable<String, NodeResourceInfo> addressmap = new Hashtable<String, NodeResourceInfo>();
    Logger log = Logger.getLogger(ExecutorServiceThread.class);

    public ExecutorServiceThread() {

    }

    Vector serviceList;
    ServerConfig serverConfig;
    MBeanServer mbeanServer;

    public void init(Vector serviceList, ServerConfig serverConfig, MBeanServer mbeanServer) {
        this.serviceList = serviceList;
        this.serverConfig = serverConfig;
        this.mbeanServer = mbeanServer;
        try {
            serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel
                    .bind(new InetSocketAddress("0.0.0.0", Integer.parseInt(serverConfig.getServicesport())));
            serverSocketChannel.configureBlocking(false);
            this.executorServiceMap = (Hashtable) mbeanServer.getAttribute(new ObjectName(warDeployerName),
                    "ExecutorServiceMap");
            this.urlClassLoaderMap = (Hashtable) mbeanServer.getAttribute(new ObjectName(warDeployerName),
                    "UrlClassLoaderMap");
            this.deployDirectory = serverConfig.getDeploydirectory();
            //log.info("Nodesport=" + nodesport);
            this.nodesport = serverConfig.getNodesport().split(",");

        } catch (Exception e) {
            log.error("Error in creating server socket channel " + serverConfig.getServicesport(), e);
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }

        log.info("initialized");

    }

    public void run() {

        // create a selector that will by used for multiplexing. The selector
        // registers the socketserverchannel as
        // well as all socketchannels that are created
        String CLIENTCHANNELNAME = "clientChannel";
        String SERVERCHANNELNAME = "serverChannel";
        String channelType = "channelType";

        ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>();
        ClassLoader classLoader = null;
        ByteArrayOutputStream bstr;
        ByteBuffer buffer = null;
        ByteBuffer lengthBuffer = ByteBuffer.allocate(4);
        int bytesRead;
        InputStream bais;
        ObjectInputStream ois;
        Object object;
        ExecutorServiceInfo executorServiceInfo = null;
        Random random = new Random(System.currentTimeMillis());
        // register the serversocketchannel with the selector. The OP_ACCEPT
        // option marks
        // a selection key as ready when the channel accepts a new connection.
        // When the
        // socket server accepts a connection this key is added to the list of
        // selected keys of the selector.
        // when asked for the selected keys, this key is returned and hence we
        // know that a new connection has been accepted.
        try {
            Selector selector = Selector.open();
            SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

            // SelectionKey socketServerSelectionKey1 =
            // channel.register(selector1,
            // SelectionKey.OP_ACCEPT);

            // set property in the key that identifies the channel
            Map<String, String> properties = new ConcurrentHashMap<String, String>();
            properties.put(channelType, SERVERCHANNELNAME);
            socketServerSelectionKey.attach(properties);
            // wait for the selected keys
            SelectionKey key = null;
            Set<SelectionKey> selectedKeys;
            // logger.info("Instance Number"+instanceNumber);
            Iterator<SelectionKey> iterator = null;
            SocketChannel clientChannel = null;
            while (true) {
                try {
                    // the select method is a blocking method which returns when
                    // atleast
                    // one of the registered
                    // channel is selected. In this example, when the socket
                    // accepts
                    // a
                    // new connection, this method
                    // will return. Once a socketclient is added to the list of
                    // registered channels, then this method
                    // would also return when one of the clients has data to be
                    // read
                    // or
                    // written. It is also possible to perform a nonblocking
                    // select
                    // using the selectNow() function.
                    // We can also specify the maximum time for which a select
                    // function
                    // can be blocked using the select(long timeout) function.

                    if (selector.select() >= 0) {
                        selectedKeys = selector.selectedKeys();
                        iterator = selectedKeys.iterator();
                    }
                    while (iterator.hasNext()) {
                        try {
                            key = iterator.next();
                            // the selection key could either by the
                            // socketserver
                            // informing
                            // that a new connection has been made, or
                            // a socket client that is ready for read/write
                            // we use the properties object attached to the
                            // channel
                            // to
                            // find
                            // out the type of channel.
                            if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) {
                                // a new connection has been obtained. This
                                // channel
                                // is
                                // therefore a socket server.
                                ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                                // accept the new connection on the server
                                // socket.
                                // Since
                                // the
                                // server socket channel is marked as non
                                // blocking
                                // this channel will return null if no client is
                                // connected.
                                SocketChannel clientSocketChannel = serverSocketChannel.accept();

                                if (clientSocketChannel != null) {
                                    // set the client connection to be non
                                    // blocking
                                    clientSocketChannel.configureBlocking(false);
                                    SelectionKey clientKey = clientSocketChannel.register(selector,
                                            SelectionKey.OP_READ, SelectionKey.OP_WRITE);
                                    Map<String, String> clientproperties = new ConcurrentHashMap<String, String>();
                                    clientproperties.put(channelType, CLIENTCHANNELNAME);
                                    clientKey.attach(clientproperties);
                                    clientKey.interestOps(SelectionKey.OP_READ);
                                    // clientSocketChannel.close();
                                    // write something to the new created client
                                    /*
                                     * CharBuffer buffer =
                                     * CharBuffer.wrap("Hello client"); while
                                     * (buffer.hasRemaining()) {
                                     * clientSocketChannel.write
                                     * (Charset.defaultCharset()
                                     * .encode(buffer)); }
                                     * clientSocketChannel.close();
                                     * buffer.clear();
                                     */
                                }

                            } else {
                                // data is available for read
                                // buffer for reading
                                clientChannel = (SocketChannel) key.channel();
                                if (key.isReadable()) {
                                    // the channel is non blocking so keep it
                                    // open
                                    // till
                                    // the
                                    // count is >=0
                                    clientChannel = (SocketChannel) key.channel();
                                    if (resultMap.get(key) == null) {
                                        //log.info(key);
                                        bstr = new ByteArrayOutputStream();
                                        object = null;
                                        clientChannel.read(lengthBuffer);
                                        int length = lengthBuffer.getInt(0);
                                        lengthBuffer.clear();
                                        //log.info(length);
                                        buffer = ByteBuffer.allocate(length);
                                        if ((bytesRead = clientChannel.read(buffer)) > 0) {
                                            // buffer.flip();
                                            // System.out
                                            // .println(bytesRead);
                                            bstr.write(buffer.array(), 0, bytesRead);
                                            buffer.clear();
                                        }
                                        buffer.clear();
                                        //log.info("Message1"+new String(bstr
                                        //      .toByteArray()));
                                        bais = new ByteArrayInputStream(bstr.toByteArray());
                                        ois = new ObjectInputStream(bais); // Offending
                                        // line.
                                        // Produces
                                        // the
                                        // StreamCorruptedException.
                                        //log.info("In read obect");
                                        object = ois.readObject();
                                        //log.info("Class Cast");
                                        //log.info("Class Cast1");
                                        ois.close();
                                        byte[] params = bstr.toByteArray();
                                        bstr.close();
                                        //log.info("readObject");
                                        //log.info("After readObject");
                                        if (object instanceof CloseSocket) {
                                            resultMap.remove(key);
                                            clientChannel.close();
                                            key.cancel();
                                        }
                                        // clientChannel.close();
                                        String serviceurl = (String) object;
                                        String[] serviceRegistry = serviceurl.split("/");
                                        //log.info("classLoaderMap"
                                        //      + urlClassLoaderMap);
                                        //log.info(deployDirectory
                                        //      + "/" + serviceRegistry[0]);

                                        int servicenameIndex;
                                        //log.info(earServicesDirectory
                                        //      + "/" + serviceRegistry[0]
                                        //      + "/" + serviceRegistry[1]);
                                        if (serviceRegistry[0].endsWith(".ear")) {
                                            classLoader = (VFSClassLoader) urlClassLoaderMap.get(deployDirectory
                                                    + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]);
                                            servicenameIndex = 2;
                                        } else if (serviceRegistry[0].endsWith(".jar")) {
                                            classLoader = (WebClassLoader) urlClassLoaderMap
                                                    .get(deployDirectory + "/" + serviceRegistry[0]);
                                            servicenameIndex = 1;
                                        } else {
                                            classLoader = (WebClassLoader) urlClassLoaderMap
                                                    .get(deployDirectory + "/" + serviceRegistry[0]);
                                            servicenameIndex = 1;
                                        }
                                        String serviceName = serviceRegistry[servicenameIndex];
                                        // log.info("servicename:"+serviceName);;
                                        synchronized (executorServiceMap) {
                                            executorServiceInfo = (ExecutorServiceInfo) executorServiceMap
                                                    .get(serviceName.trim());
                                        }
                                        ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader();
                                        classLoaderExecutorServiceInfo.setClassLoader(classLoader);
                                        classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo);
                                        resultMap.put(key, classLoaderExecutorServiceInfo);
                                        // key.interestOps(SelectionKey.OP_READ);
                                        // log.info("Key interested Ops");
                                        // continue;
                                    }
                                    //Thread.sleep(100);
                                    /*
                                     * if (classLoader == null) throw new
                                     * Exception(
                                     * "Could able to obtain deployed class loader"
                                     * );
                                     */
                                    /*
                                     * log.info(
                                     * "current context classloader" +
                                     * classLoader);
                                     */
                                    //log.info("In rad object");
                                    bstr = new ByteArrayOutputStream();
                                    lengthBuffer.clear();
                                    int numberofDataRead = clientChannel.read(lengthBuffer);
                                    //log.info("numberofDataRead"
                                    //      + numberofDataRead);
                                    int length = lengthBuffer.getInt(0);
                                    if (length <= 0) {
                                        iterator.remove();
                                        continue;
                                    }
                                    lengthBuffer.clear();
                                    //log.info(length);
                                    buffer = ByteBuffer.allocate(length);
                                    buffer.clear();
                                    if ((bytesRead = clientChannel.read(buffer)) > 0) {
                                        // buffer.flip();
                                        // System.out
                                        // .println(bytesRead);
                                        bstr.write(buffer.array(), 0, bytesRead);
                                        buffer.clear();
                                    }
                                    if (bytesRead <= 0 || bytesRead < length) {
                                        //log.info("bytesRead<length");
                                        iterator.remove();
                                        continue;
                                    }
                                    //log.info(new String(bstr
                                    //   .toByteArray()));
                                    bais = new ByteArrayInputStream(bstr.toByteArray());

                                    ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap
                                            .get(key);
                                    ois = new ClassLoaderObjectInputStream(
                                            (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending
                                    // line.
                                    // Produces
                                    // the
                                    // StreamCorruptedException.
                                    object = ois.readObject();
                                    ois.close();
                                    bstr.close();
                                    executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo();
                                    //System.out
                                    //      .println("inputStream Read Object");
                                    //log.info("Object="
                                    //      + object.getClass());
                                    // Thread.currentThread().setContextClassLoader(currentContextLoader);
                                    if (object instanceof ExecutorParams) {
                                        ExecutorParams exeParams = (ExecutorParams) object;
                                        Object returnValue = null;

                                        //log.info("test socket1");
                                        String ataKey;
                                        ATAConfig ataConfig;
                                        ConcurrentHashMap ataServicesMap;
                                        Enumeration<NodeResourceInfo> noderesourceInfos = addressmap.elements();
                                        NodeResourceInfo noderesourceinfo = null;
                                        String ip = "";
                                        int port = 1000;
                                        long memavailable = 0;
                                        long memcurr = 0;
                                        if (noderesourceInfos.hasMoreElements()) {
                                            noderesourceinfo = noderesourceInfos.nextElement();
                                            if (noderesourceinfo.getMax() != null) {
                                                ip = noderesourceinfo.getHost();
                                                port = Integer.parseInt(noderesourceinfo.getPort());
                                                memavailable = Long.parseLong(noderesourceinfo.getMax())
                                                        - Long.parseLong(noderesourceinfo.getUsed());
                                                ;
                                            }
                                        }

                                        while (noderesourceInfos.hasMoreElements()) {
                                            noderesourceinfo = noderesourceInfos.nextElement();
                                            if (noderesourceinfo.getMax() != null) {
                                                memcurr = Long.parseLong(noderesourceinfo.getMax())
                                                        - Long.parseLong(noderesourceinfo.getUsed());
                                                if (memavailable <= memcurr) {
                                                    ip = noderesourceinfo.getHost();
                                                    port = Integer.parseInt(noderesourceinfo.getPort());
                                                    memavailable = memcurr;
                                                }
                                            }
                                        }
                                        ATAExecutorServiceInfo servicesAvailable;
                                        Socket sock1 = new Socket(ip, port);
                                        OutputStream outputStr = sock1.getOutputStream();
                                        ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr);
                                        NodeInfo nodeInfo = new NodeInfo();
                                        nodeInfo.setClassNameWithPackage(
                                                executorServiceInfo.getExecutorServicesClass().getName());
                                        nodeInfo.setMethodName(executorServiceInfo.getMethod().getName());

                                        nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS());
                                        NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam();
                                        nodeInfoMethodParam.setMethodParams(exeParams.getParams());
                                        nodeInfoMethodParam
                                                .setMethodParamTypes(executorServiceInfo.getMethodParams());
                                        //log.info("Serializable socket="+sock);
                                        //nodeInfo.setSock(sock);
                                        //nodeInfo.setOstream(sock.getOutputStream());
                                        objOutputStream.writeObject(nodeInfo);
                                        objOutputStream = new ObjectOutputStream(outputStr);
                                        objOutputStream.writeObject(nodeInfoMethodParam);
                                        ObjectInputStream objInputStream1 = new ObjectInputStream(
                                                sock1.getInputStream());
                                        returnValue = objInputStream1.readObject();
                                        objOutputStream.close();
                                        objInputStream1.close();
                                        sock1.close();
                                        /*returnValue = executorServiceInfo
                                              .getMethod()
                                              .invoke(executorServiceInfo
                                                    .getExecutorServicesClass()
                                                    .newInstance(),
                                                    exeParams.getParams());*/
                                        // Thread.currentThread().setContextClassLoader(oldCL);

                                        //   log.info("Written Value="
                                        //         + returnValue.toString());
                                        resultMap.put(key, returnValue);
                                    }
                                    key.interestOps(SelectionKey.OP_WRITE);
                                    //log.info("Key interested Ops1");
                                } else if (key.isWritable()) {
                                    // the channel is non blocking so keep it
                                    // open
                                    // till the
                                    // count is >=0
                                    //log.info("In write");
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make
                                    // a
                                    // BAOS
                                    // stream
                                    ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the
                                                                                           // stream
                                    Object result = resultMap.get(key);
                                    oos.writeObject(result); // write an object
                                    // to
                                    // the stream
                                    oos.flush();
                                    oos.close();
                                    byte[] objData = baos.toByteArray(); // get
                                    // the
                                    // byte
                                    // array
                                    baos.close();
                                    buffer = ByteBuffer.wrap(objData); // wrap
                                    // around
                                    // the
                                    // data
                                    buffer.rewind();
                                    // buffer.flip(); //prep for writing
                                    //log.info(new String(objData));
                                    //while (buffer.hasRemaining())
                                    clientChannel.write(buffer); // write
                                    resultMap.remove(key);
                                    buffer.clear();
                                    key.cancel();
                                    clientChannel.close();
                                    //log.info("In write1");
                                    numberOfServicesRequests++;
                                    //log.info("Key interested Ops2");
                                }

                            }

                            iterator.remove();
                        } catch (Exception ex) {
                            log.error("Error in executing the executor services thread", ex);
                            //ex.printStackTrace();
                            key.cancel();
                            clientChannel.close();
                            resultMap.remove(key);
                            //ex.printStackTrace();
                        }

                    }

                } catch (Exception ex) {
                    log.error("Error in executing the executor services thread", ex);
                    //ex.printStackTrace();
                }
            }
        } catch (Exception ex) {
            log.error("Error in executing the executor services thread", ex);
        }
    }

    /**
     * This method is the implementation of the executor service which runs now
     * through the Node Server
     */
    /*public void run1() {
        
       ServerSocket serverSocket = null;
       Socket sock;
       try {
     serverSocket = new ServerSocket(this.requestPort, 500);
     log.info("serverSocket" + serverSocket);
       } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
       }
       java.util.concurrent.ExecutorService executor = new java.util.concurrent.ScheduledThreadPoolExecutor(
        500);
       while (true) {
        
     try {
        sock = (Socket) serverSocket.accept();
        log.info("A new Socket=" + sock);
        ExecutorServiceProcessor executorServiceProcessor = new ExecutorServiceProcessor(
              sock, executorServiceMap, urlClassLoaderMap,
              deployDirectory);
        executor.execute(executorServiceProcessor);
        // executorServiceProcessor.start();
     } catch (Exception ex) {
        try {
           Socket socket = new Socket("localhost", shutDownPort);
           OutputStream outputStream = socket.getOutputStream();
           outputStream.write("shutdown WebServer\r\n\r\n".getBytes());
           outputStream.close();
        } catch (IOException e1) {
        
           e1.printStackTrace();
        }
     }
       }
    }*/

    /**
     * This class is the implementation of the Executor service processor.
     * 
     * @author arun
     * 
     */

    /**
     * This method returns the number of executor service requests.
     */

    public int getNumberOfServicesRequests() {

        return numberOfServicesRequests;
    }

    class ExecutorServiceInfoClassLoader {
        ExecutorServiceInfo executorServiceInfo;
        ClassLoader classLoader;

        public ExecutorServiceInfo getExecutorServiceInfo() {
            return executorServiceInfo;
        }

        public void setExecutorServiceInfo(ExecutorServiceInfo executorServiceInfo) {
            this.executorServiceInfo = executorServiceInfo;
        }

        public ClassLoader getClassLoader() {
            return classLoader;
        }

        public void setClassLoader(ClassLoader classLoader) {
            this.classLoader = classLoader;
        }
    }

    public void setObjectName(ObjectName objName) {
        this.objName = objName;
    }

    public ObjectName getObjectName() {

        return this.objName;
    }

    public void start() {
        schexc = Executors.newSingleThreadScheduledExecutor();
        schexc.execute(this);
        log.info("started");

    }

    public void stop() {
        schexc.shutdownNow();
        log.info("stopped");

    }

    public void destroy() {
        log.info("destroyed");
    }

    public void setWarDeployerName(String warDeployerName) {
        this.warDeployerName = warDeployerName;
    }

    public void setDeployer(String isdeployer) {
        this.isdeployer = isdeployer;
    }

    public String getDeployer() {
        return this.isdeployer;
    }

    public Hashtable<String, NodeResourceInfo> getNodeResourceInfo() {
        // TODO Auto-generated method stub
        return this.addressmap;
    }
}