@@ -312,93 +312,171 @@ public class EventProducerThread extends DatabusThreadBase implements Transactio
312312
313313 private String _sourceName ;
314314
315+ private final long _reconnectIntervalMs = 2000 ;
316+ private final long _workIntervalMs = 100 ;
317+
315318 public EventProducerThread (String sourceName , long sinceScn )
316319 {
317320 super ("OpenReplicator_" + sourceName );
318321 _sourceName = sourceName ;
319322 _sinceScn = sinceScn ;
320323 }
321324
322- @ Override
323- public void run ()
325+ void initOpenReplicator (long scn )
324326 {
325- _eventBuffer .start (_sinceScn );
326- _startPrevScn .set (_sinceScn );
327-
328- int offset = offset (_sinceScn );
329- int logid = logid (_sinceScn );
327+ int offset = offset (scn );
328+ int logid = logid (scn );
330329
331330 String binlogFile = String .format ("%s.%06d" , _binlogFilePrefix , logid );
332- _orListener = new ORListener ( _sourceName , logid , _log ,
333- _binlogFilePrefix , _producerThread , _tableUriToSrcIdMap ,
331+ // we should use a new ORListener to drop the left events in binlogEventQueue and the half processed transaction.
332+ _orListener = new ORListener ( _sourceName , logid , _log , _binlogFilePrefix , _producerThread , _tableUriToSrcIdMap ,
334333 _tableUriToSrcNameMap , _schemaRegistryService , 200 );
335334
336335 _or .setBinlogFileName (binlogFile );
337336 _or .setBinlogPosition (offset );
338337 _or .setBinlogEventListener (_orListener );
339338
339+ //must set transport and binlogParser to null to drop the old connection environment in reinit case
340+ _or .setTransport (null );
341+ _or .setBinlogParser (null );
342+
343+ _log .info (String .format ("Open Replicator starting from %s@%d" , binlogFile , offset ));
344+ }
345+
346+ @ Override
347+ public void run ()
348+ {
349+ _eventBuffer .start (_sinceScn );
350+ _startPrevScn .set (_sinceScn );
351+
352+ initOpenReplicator (_sinceScn );
340353 try
341354 {
342- _log .info (String .format ("Open Replicator starting from %s@%d" , binlogFile , offset ));
343355 _or .start ();
344356 _orListener .start ();
345357 } catch (Exception e )
346358 {
347359 throw new DatabusRuntimeException ("failed to start open replicator: " + e .getMessage (), e );
348360 }
349- _log .info ("Event Producer Thread done" );
350361
351- }
352-
353- @ Override
354- public void onEndTransaction (Transaction txn )
355- throws DatabusException
356- {
357- if (! isShutdownRequested ())
362+ long lastConnectMs = System .currentTimeMillis ();
363+ while (!isShutdownRequested ())
358364 {
359365 if (isPauseRequested ())
360366 {
361367 LOG .info ("Pause requested for OpenReplicator. Pausing !!" );
362368 signalPause ();
363369 LOG .info ("Pausing. Waiting for resume command" );
364- try { awaitUnPauseRequest (); } catch (InterruptedException e ) { _log .info ("Interrupted !!" ); }
370+ try
371+ {
372+ if (_orListener .isAlive ())
373+ {
374+ _orListener .pause ();
375+ }
376+ awaitUnPauseRequest ();
377+ }
378+ catch (InterruptedException e )
379+ {
380+ _log .info ("Interrupted !!" );
381+ }
365382 LOG .info ("Resuming OpenReplicator !!" );
383+ if (_orListener .isAlive ())
384+ {
385+ try
386+ {
387+ _orListener .unpause ();
388+ }
389+ catch (InterruptedException e )
390+ {
391+ _log .info ("Interrupted !!" );
392+ }
393+ }
366394 signalResumed ();
367395 LOG .info ("OpenReplicator resumed !!" );
368396 }
369397
370- try
398+ if (! _or . isRunning () && System . currentTimeMillis () - lastConnectMs > _reconnectIntervalMs )
371399 {
372- addTxnToBuffer (txn );
373- _maxSCNReaderWriter .saveMaxScn (txn .getScn ());
400+ lastConnectMs = System .currentTimeMillis ();
401+ try
402+ {
403+ //should stop orListener first to get the final maxScn used for init open replicator.
404+ if (_orListener .isAlive ())
405+ {
406+ _orListener .shutdown ();
407+ }
408+ long maxScn = _maxSCNReaderWriter .getMaxScn ();
409+ _startPrevScn .set (maxScn );
410+ initOpenReplicator (maxScn );
411+ _or .start ();
412+ _orListener .start ();
413+ _log .info ("start Open Replicator successfully" );
414+ }
415+ catch (Exception e )
416+ {
417+ _log .error ("failed to start Open Replicator" , e );
418+ if (_or .isRunning ())
419+ {
420+ try
421+ {
422+ _or .stop (10 , TimeUnit .SECONDS );
423+ }
424+ catch (Exception e2 )
425+ {
426+ _log .error ("failed to stop Open Replicator" , e2 );
427+ }
428+ }
429+ }
374430 }
375- catch (UnsupportedKeyException e )
431+
432+ try
376433 {
377- _log .fatal ("Got UnsupportedKeyException exception while adding txn (" + txn + ") to the buffer" , e );
378- throw new DatabusException (e );
434+ Thread .sleep (_workIntervalMs );
379435 }
380- catch (EventCreationException e )
436+ catch (InterruptedException e )
381437 {
382- _log .fatal ("Got EventCreationException exception while adding txn (" + txn + ") to the buffer" , e );
383- throw new DatabusException (e );
438+ _log .info ("Interrupted !!" );
384439 }
385440 }
386441
387- if (isShutdownRequested ())
442+ if (_or . isRunning ())
388443 {
389444 try
390445 {
391- // Because the current thread is orListener thread, so shutdown() will block the thread itself.
392- // So we just set orListener's shutdown flag, the orListener thead will exit immediately after this function.
393- _orListener .shutdownAsynchronously ();
394446 _or .stop (10 , TimeUnit .SECONDS );
395447 }
396448 catch (Exception e )
397449 {
398- _log .error ("Got exception while stopping open replicator" , e );
450+ _log .error ("failed to stop Open Replicator" , e );
399451 }
400- doShutdownNotify ();
401- return ;
452+ }
453+ if (_orListener .isAlive ())
454+ {
455+ _orListener .shutdown ();
456+ }
457+
458+ _log .info ("Event Producer Thread done" );
459+ doShutdownNotify ();
460+ }
461+
462+ @ Override
463+ public void onEndTransaction (Transaction txn )
464+ throws DatabusException
465+ {
466+ try
467+ {
468+ addTxnToBuffer (txn );
469+ _maxSCNReaderWriter .saveMaxScn (txn .getScn ());
470+ }
471+ catch (UnsupportedKeyException e )
472+ {
473+ _log .fatal ("Got UnsupportedKeyException exception while adding txn (" + txn + ") to the buffer" , e );
474+ throw new DatabusException (e );
475+ }
476+ catch (EventCreationException e )
477+ {
478+ _log .fatal ("Got EventCreationException exception while adding txn (" + txn + ") to the buffer" , e );
479+ throw new DatabusException (e );
402480 }
403481 }
404482
0 commit comments