pausinghandlerexecutiontest.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 458 行 · 第 1/2 页

JAVA
458
字号
                System.out.println(failmsg);
                throw new AxisFault(failmsg + "   Handler failed");
            }

            // add this handler to the list of invoked handlers
            System.out.println(title + " adding this handler to testResults list");
            testResults.add(handlerName);

            boolean isPaused = msgContext.isPaused();
            if (isPaused) {
                System.out.println(title + "   message context is paused   *****");
            } else {
                System.out.println(title + "   message context is not paused");
            }

            checkLists(msgContext);

            // check if the handler should pause
            if (shouldPause) {
                String pausemsg = title + " - pausing the message context";
                System.out.println(pausemsg);
                msgContext.pause();
                shouldPause = false;

                File theFile = null;
                String theFilename = null;

                try {
                    theFile = File.createTempFile("pHexec", null);
                    theFilename = theFile.getName();
                    System.out.println(title + "temp file = [" + theFilename + "]");
                }
                catch (Exception ex) {
                    System.out.println(
                            title + "error creating temp file = [" + ex.getMessage() + "]");
                    theFile = null;
                }

                if (theFile != null) {
                    // save to the temporary file
                    try {
                        // setup an output stream to a physical file
                        FileOutputStream outStream = new FileOutputStream(theFile);

                        // attach a stream capable of writing objects to the
                        // stream connected to the file
                        ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);

                        // try to save the message context
                        System.out.println(title + "saving message context.....");
                        outObjStream.writeObject(msgContext);

                        // close out the streams
                        outObjStream.flush();
                        outObjStream.close();
                        outStream.flush();
                        outStream.close();

                        System.out.println(title + "....saved message context.....");

                        long filesize = theFile.length();
                        System.out.println(title + "file size after save [" + filesize +
                                "]   temp file = [" + theFilename + "]");

                        new Worker(theFile, msgContext.getConfigurationContext()).start();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                        fail("An error occurred when serializing the MessageContext");
                    }
                } else {
                    // couldn't get a temporary file
                    new Worker(msgContext, msgContext.getConfigurationContext()).start();
                }
                return InvocationResponse.SUSPEND;
            } // end if should pause

            return InvocationResponse.CONTINUE;
        }

        public void flowComplete(MessageContext msgContext) {
            String title = "TestHandler[" + handlerName + "] ";
            String label = "FC" + handlerName;
            System.out
                    .println(title + " flowComplete(): adding [" + label + "] to testResults list");
            testResults.add("FC" + handlerName);
        }

        private void checkLists(MessageContext mc) {
            if (mc == null) {
                return;
            }

            String title = "TestHandler[" + handlerName + "] ";

            System.out.println(title + "Checking execution chain.....");
            ArrayList execList = mc.getExecutionChain();
            Iterator execIt = null;
            if (execList != null) {
                execIt = execList.iterator();
            }
            checkHandler(execIt);

            System.out.println(title + "Checking inbound executed phases list.....");
            Iterator inboundIt = mc.getExecutedPhases();
            checkHandler(inboundIt);
        }

        private void checkHandler(Iterator it) {
            if (it == null) {
                return;
            }

            while (it.hasNext()) {
                Handler handler = (Handler)it.next();

                if (handler instanceof Handler) {
                    System.out.println("Handler name [" + handler.getName() + "]");
                } else if (handler instanceof Phase) {
                    Phase phase = (Phase)handler;
                    System.out.println("Phase name [" + phase.getName() + "]");

                    ArrayList list2 = phase.getHandlers();
                    Iterator it2 = list2.iterator();
                    checkHandler(it2);
                }
            }

        }


    }

    private class Worker extends Thread {
        private byte[] serializedMessageContext = null;
        private ConfigurationContext configurationContext = null;
        private File theFile = null;
        private String theFilename = null;
        private MessageContext msgContext = null;

        public Worker(MessageContext msgContext) {
            this.msgContext = msgContext;
            this.configurationContext = msgContext.getConfigurationContext();
        }

        public Worker(byte[] serializedMessageContext, ConfigurationContext configurationContext) {
            this.serializedMessageContext = serializedMessageContext;
            this.configurationContext = configurationContext;
        }

        public Worker(File theFile, ConfigurationContext configurationContext) {
            this.theFile = theFile;
            this.configurationContext = configurationContext;
        }

        public Worker(MessageContext mc, ConfigurationContext configurationContext) {
            this.msgContext = mc;
            this.configurationContext = configurationContext;
        }


        public void run() {
            try {
                System.out.println("Worker thread started");
                Thread.sleep(5000);
                AxisEngine axisEngine = new AxisEngine(configurationContext);

                FileInputStream inStream = null;
                ObjectInputStream objectInputStream = null;
                MessageContext reconstitutedMessageContext = null;

                if (theFile != null) {
                    // setup an input stream to the file
                    inStream = new FileInputStream(theFile);

                    // attach a stream capable of reading objects from the
                    // stream connected to the file
                    objectInputStream = new ObjectInputStream(inStream);

                    System.out.println("Worker thread restoring message context from file");
                    reconstitutedMessageContext = (MessageContext)objectInputStream.readObject();
                    reconstitutedMessageContext.activate(configurationContext);
                } else if (serializedMessageContext != null) {
                    // use the byte array
                    objectInputStream = new ObjectInputStream(
                            new ByteArrayInputStream(serializedMessageContext));

                    System.out.println("Worker thread restoring message context from byte array");
                    reconstitutedMessageContext = (MessageContext)objectInputStream.readObject();
                    reconstitutedMessageContext.activate(configurationContext);
                } else if (msgContext != null) {
                    System.out.println("Worker thread using message context");
                    reconstitutedMessageContext = msgContext;
                }

                if (inStream != null) {
                    inStream.close();
                }

                if (objectInputStream != null) {
                    objectInputStream.close();
                }

                if (theFile != null) {
                    // remove the temporary file
                    try {
                        theFile.delete();
                    }
                    catch (Exception e) {
                        // just absorb it
                    }
                }

                if (reconstitutedMessageContext != null) {
                    System.out.println("Worker thread resuming message context");
                    axisEngine.resume(reconstitutedMessageContext);
                } else {
                    fail("An error occurred in the worker thread - no message context");
                }

            }
            catch (Exception e) {
                e.printStackTrace();
                fail("An error occurred in the worker thread");
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?