2727import java .util .HashMap ;
2828import java .util .List ;
2929import java .util .Map ;
30- import java .util .Timer ;
31- import java .util .TimerTask ;
3230import java .util .UUID ;
31+ import java .util .concurrent .Executors ;
32+ import java .util .concurrent .ScheduledExecutorService ;
33+ import java .util .concurrent .TimeUnit ;
3334import java .util .stream .Collectors ;
3435
3536import javax .inject .Inject ;
5253import org .apache .cloudstack .framework .jobs .AsyncJobManager ;
5354import org .apache .cloudstack .framework .jobs .impl .VmWorkJobVO ;
5455import org .apache .cloudstack .jobs .JobInfo ;
55- import org .apache .cloudstack .managed .context .ManagedContextTimerTask ;
56+ import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
5657import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
5758import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
5859import org .apache .commons .collections .MapUtils ;
8384import com .cloud .user .User ;
8485import com .cloud .utils .NumbersUtil ;
8586import com .cloud .utils .Pair ;
87+ import com .cloud .utils .concurrency .NamedThreadFactory ;
8688import com .cloud .utils .ReflectionUse ;
8789import com .cloud .utils .component .ManagerBase ;
8890import com .cloud .utils .exception .CloudRuntimeException ;
@@ -140,7 +142,7 @@ public class KVMBackupExportServiceImpl extends ManagerBase implements KVMBackup
140142 @ Inject
141143 AsyncJobManager asyncJobManager ;
142144
143- private Timer imageTransferTimer ;
145+ private ScheduledExecutorService imageTransferStatusExecutor ;
144146
145147 VmWorkJobHandlerProxy jobHandlerProxy = new VmWorkJobHandlerProxy (this );
146148
@@ -884,7 +886,9 @@ private ImageTransferResponse toImageTransferResponse(ImageTransferVO imageTrans
884886
885887 @ Override
886888 public boolean start () {
887- final TimerTask imageTransferPollTask = new ManagedContextTimerTask () {
889+ imageTransferStatusExecutor = Executors .newScheduledThreadPool (1 , new NamedThreadFactory ("Image-Transfer-Status-Executor" ));
890+ long pollingInterval = ImageTransferPollingInterval .value ();
891+ imageTransferStatusExecutor .scheduleAtFixedRate (new ManagedContextRunnable () {
888892 @ Override
889893 protected void runInContext () {
890894 try {
@@ -893,20 +897,13 @@ protected void runInContext() {
893897 logger .warn ("Catch throwable in image transfer poll task " , t );
894898 }
895899 }
896- };
897-
898- imageTransferTimer = new Timer ("ImageTransferPollTask" );
899- long pollingInterval = ImageTransferPollingInterval .value () * 1000L ;
900- imageTransferTimer .schedule (imageTransferPollTask , pollingInterval , pollingInterval );
900+ }, pollingInterval , pollingInterval , TimeUnit .SECONDS );
901901 return true ;
902902 }
903903
904904 @ Override
905905 public boolean stop () {
906- if (imageTransferTimer != null ) {
907- imageTransferTimer .cancel ();
908- imageTransferTimer = null ;
909- }
906+ imageTransferStatusExecutor .shutdown ();
910907 return true ;
911908 }
912909
@@ -973,7 +970,7 @@ private void pollImageTransferProgress() {
973970 VolumeVO volume = volumeDao .findById (transfer .getVolumeId ());
974971 if (volume == null ) {
975972 logger .warn ("Volume not found for image transfer: {}" , transfer .getUuid ());
976- imageTransferDao .remove (transfer .getId ()); // ToDo: confirm if this enough?
973+ imageTransferDao .remove (transfer .getId ());
977974 continue ;
978975 }
979976 transferVolumeMap .put (transfer .getId (), volume );
@@ -1000,8 +997,9 @@ private void pollImageTransferProgress() {
1000997 if (answer == null || !answer .getResult () || MapUtils .isEmpty (answer .getProgressMap ())) {
1001998 logger .warn ("Failed to get progress for transfers on host {}: {}" , hostId ,
1002999 answer != null ? answer .getDetails () : "null answer" );
1003- return ; // ToDo: return on continue?
1000+ continue ;
10041001 }
1002+
10051003 for (ImageTransferVO transfer : hostTransfers ) {
10061004 String transferId = transfer .getUuid ();
10071005 Long currentSize = answer .getProgressMap ().get (transferId );
0 commit comments