替代无限循环作为监听器

Xan*_*hos 2 java java-ee-6 ibm-midrange

好吧,也许这对我来说是一个小问题.但我想问一下这个问题.我有一个Java Web应用程序,它通过无限循环检查来自AS400的DataQueue.如果队列中有消息,它会将消息传递给MQ,如果没有,只需继续检查.

起初这是一个好主意,但似乎当我在WAS中部署这个Web应用程序(ServletContextListener)并启动它时,我无法阻止它.也许是因为它消耗了资源.

所以也许无限循环不是答案.您是否知道在AS400 DataQueue上不断检查新消息的方法?

Cha*_*les 5

您不需要经常检查或手动暂停..

您可以将超时值传递给read(),并且您的app/thread将在返回之前等待那么长的条目.如果你传递-1,它会永远等待......

来自
http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/topic/rzahh/dqconsumerexample.htm#dqconsumerexample

         // Read the first entry off the queue.  The timeout value is
         // set to -1 so this program will wait forever for an entry.
         System.out.println("*** Waiting for an entry for process ***");

         DataQueueEntry DQData = dq.read(-1);

         while (Continue)
         {

            // We just read an entry off the queue.  Put the data into
            // a record object so the program can access the fields of
            // the data by name.  The Record object will also convert
            // the data from server format to Java format.
            Record data = dataFormat.getNewRecord(DQData.getData());

            // Get two values out of the record and display them.
            Integer amountOrdered = (Integer) data.getField("QUANTITY");
            String  partOrdered   = (String)  data.getField("PART_NAME");

            System.out.println("Need " + amountOrdered + " of "
                               + partOrdered);
            System.out.println(" ");
            System.out.println("*** Waiting for an entry for process ***");

            // Wait for the next entry.
            DQData = dq.read(-1);
         }
Run Code Online (Sandbox Code Playgroud)

  • 由于操作系统的性质,在IBM i(fka AS400)上,处于等待状态的作业或线程对性能的影响基本上是*NO*.在等待或过期之前,它不会变为活动状态.在-1的情况下,它不会过期.不需要循环,也不用担心CPU. (3认同)