3

我有一个问题,我的班级正在执行第一个运行方法,之后它没有进入第二个覆盖的运行方法。

程序执行存在于具有 main 方法和线程池的控制器类中:

public class RunnableController {
    // Main method
    public static void main(String[] args) throws InterruptedException {
        try {
            RunnableController controller = new RunnableController();
            controller.initializeDb();
            controller.initialiseThreads();
            System.out.println("Polling");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initialiseThreads() {      
        try {
            threadExecutorRead = Executors.newFixedThreadPool(10);
            PollingSynchronizer read = new PollingSynchronizer(incomingQueue, dbConncetion);
            threadExecutorRead.submit(read);
        } catch (Exception e){
            e.printStackTrace();
        }   
    }
}

我的 poller 类获取新数据并应该模拟更新:

public class PollingSynchronizer implements Runnable {
   public PollingSynchronizer(Collection<KamMessage> incomingQueue,
         Connection dbConnection) {
      super();
      this.incomingQueue = incomingQueue;
      this.dbConnection = dbConnection;
   }

   private int seqId;

   public int getSeqId() {
      return seqId;
   }

   public void setSeqId(int seqId) {
      this.seqId = seqId;
   }

   // The method which runs Polling action and record the time at which it is done
   public void run() {
      int seqId = 0;

      while (true) {
         List<KamMessage> list = null;

         try {
            list = fullPoll(seqId);

            if (!list.isEmpty()) {
               seqId = list.get(0).getSequence();
               incomingQueue.addAll(list);
               this.outgoingQueue = incomingQueue;
               System.out.println("waiting 3 seconds");
               System.out.println("new incoming message");
               Thread.sleep(3000);//at this wait I should execute run()

               //when I debug my execution stops here and throws " Class not found Exception "
               // its does not enters the message processor class 
               MessageProcessor processor = new MessageProcessor() {
                  //the run method which should fetch the message processor class.
                  final public void run() {
                     MessageProcessor(outgoingQueue).generate(outgoingQueue);
                  }
               };
              new Thread(processor).start();
            }
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
}

我的消息处理器类:

public abstract class MessageProcessor implements Runnable {
   private Connection dbConnection;
   Statement st = null;
   ResultSet rs = null;
   PreparedStatement pstmt = null;
   private Collection<KamMessage> outgoingQueue;

   public KamMsg804 MessageProcessor(Collection<KamMessage> outgoingQueue,
         Connection dbConnection) {
      this.outgoingQueue = outgoingQueue;
      this.dbConnection = dbConnection;
      return (KpiMsg804) fetchedMessages;
   }

   public Collection<KamMessage> generate(Collection<KamMessage> outgoingQueue) {
      while (true) {
         try {
            while (rs.next()) {
               KamMessage filedClass = convertRecordsetToPojo(rs);
               outgoingQueue.add(filedClass);
            }

            for (KamMessage pojoClass : outgoingQueue) {
               KamMsg804 updatedValue = createKamMsg804(pojoClass);
               System.out.print(" " + pojoClass.getSequence());
               System.out.print(" " + pojoClass.getTableName());
               System.out.print(" " + pojoClass.getAction());
               System.out.print(" " + updatedValue.getKeyInfo1());
               System.out.print(" " + updatedValue.getKeyInfo2());
               System.out.println(" " + pojoClass.getEntryTime());
            }
            return outgoingQueue;
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

我的问题恰好在第二次运行(9 方法中,我在 MessageProcessor 类中遇到异常,它循环回 Polling。

  1. 我如何在这里实现多线程,因为当线程在轮询中休眠 3 秒时,它应该同时更新数据库。
  2. 之后,如何将数据馈送并更新回数据库。

我的程序流程 - 我有三个类: 1.Controller 2.PollerSynchro 3.Msgprocessor

我有数据库记录,这些记录被转换为 POJO 形式并存储在 Collection 中。使用这些 POJO,我的课程尝试一次性进行多处理和更新。

  1. 控制器 - 拥有线程池,使用 poll 方法启动 poller 类 - 完成

  2. Poller - 应该轮询新的传入消息并将其存储在传入队列中 - 完成

  3. MsgProcessor - 应该寻找新的传入消息并将它们从传出队列传递到传入队列 - 也完成

问题:

现在我的问题是

  1. 我必须在轮询线程休眠 3 秒时执行此更新,

  2. 在我的 Poller 类中的第二个 void run() 方法的代码中,传出队列没有传递给消息处理器类进行更新。我的执行流程只是循环回到第一个运行方法并得到类异常。

请帮我解决这些问题。

4

1 回答 1

3

我不能粉饰这个,你的代码是一团糟。但是,至于为什么您的消息处理器代码没有被执行,您实际上从未启动使用此代码创建的线程:

MessageProcessor processor = new MessageProcessor() {
    // the run method which should fetch the message processor class.
    final public void run() {
         MessageProcessor(outgoingQueue).generate(outgoingQueue);                    
    }
};

忽略被调用的混淆命名方法,您的代码应该更像这样:

Message processor = new MessageProcessor() {
    // the run method which should fetch the message processor class.
    final public void run() {
         MessageProcessor(outgoingQueue).generate(outgoingQueue);                    
    }
};

new Thread(processor).start();
于 2013-01-18T15:31:09.957 回答