@@ -342,108 +342,108 @@ public Object execute(ActionContext context) throws Exception {
342342 }
343343
344344 private int doTransfer (ActionContext context ) throws Exception {
345- ConnectionFactoryClosable sourceConnectionFactory = createConnectionFactory ("source" , sourceProtocol , sourceURL , sourceUser , sourcePassword , sourceClientID );
346- Connection sourceConnection = sourceConnectionFactory .createConnection ();
347-
348- Session sourceSession = sourceConnection .createSession (Session .SESSION_TRANSACTED );
349- Destination sourceDestination = createDestination ("source" , sourceSession , sourceQueue , sourceTopic );
350- MessageConsumer consumer = null ;
351- if (sourceDestination instanceof Queue ) {
352- if (filter != null ) {
353- consumer = sourceSession .createConsumer (sourceDestination , filter );
354- } else {
355- consumer = sourceSession .createConsumer (sourceDestination );
356- }
357- } else if (sourceDestination instanceof Topic topic ) {
345+ try (ConnectionFactoryClosable sourceConnectionFactory = createConnectionFactory ("source" , sourceProtocol , sourceURL , sourceUser , sourcePassword , sourceClientID );
346+ Connection sourceConnection = sourceConnectionFactory .createConnection ();
347+ Session sourceSession = sourceConnection .createSession (Session .SESSION_TRANSACTED )) {
358348
359- if (durableConsumer != null ) {
360- if (filter != null ) {
361- consumer = sourceSession .createDurableConsumer (topic , durableConsumer );
362- } else {
363- consumer = sourceSession .createDurableConsumer (topic , durableConsumer , filter , noLocal );
364- }
365- } else if (sharedDurableSubscription != null ) {
349+ Destination sourceDestination = createDestination ("source" , sourceSession , sourceQueue , sourceTopic );
350+ MessageConsumer consumer = null ;
351+ if (sourceDestination instanceof Queue ) {
366352 if (filter != null ) {
367- consumer = sourceSession .createSharedDurableConsumer ( topic , sharedDurableSubscription , filter );
353+ consumer = sourceSession .createConsumer ( sourceDestination , filter );
368354 } else {
369- consumer = sourceSession .createSharedDurableConsumer ( topic , sharedDurableSubscription );
355+ consumer = sourceSession .createConsumer ( sourceDestination );
370356 }
371- } else if (sharedSubscription != null ) {
372- if (filter != null ) {
373- consumer = sourceSession .createSharedConsumer (topic , sharedSubscription , filter );
357+ } else if (sourceDestination instanceof Topic topic ) {
358+
359+ if (durableConsumer != null ) {
360+ if (filter != null ) {
361+ consumer = sourceSession .createDurableConsumer (topic , durableConsumer );
362+ } else {
363+ consumer = sourceSession .createDurableConsumer (topic , durableConsumer , filter , noLocal );
364+ }
365+ } else if (sharedDurableSubscription != null ) {
366+ if (filter != null ) {
367+ consumer = sourceSession .createSharedDurableConsumer (topic , sharedDurableSubscription , filter );
368+ } else {
369+ consumer = sourceSession .createSharedDurableConsumer (topic , sharedDurableSubscription );
370+ }
371+ } else if (sharedSubscription != null ) {
372+ if (filter != null ) {
373+ consumer = sourceSession .createSharedConsumer (topic , sharedSubscription , filter );
374+ } else {
375+ consumer = sourceSession .createSharedConsumer (topic , sharedSubscription );
376+ }
374377 } else {
375- consumer = sourceSession . createSharedConsumer ( topic , sharedSubscription );
378+ throw new IllegalArgumentException ( "you must specify either --durable-consumer, --shared-durable-subscription or --shared-subscription with a JMS topic" );
376379 }
377- } else {
378- throw new IllegalArgumentException ("you must specify either --durable-consumer, --shared-durable-subscription or --shared-subscription with a JMS topic" );
379- }
380- }
381-
382- ConnectionFactoryClosable targetConnectionFactory = createConnectionFactory ("target" , targetProtocol , targetURL , targetUser , targetPassword , null );
383- Connection targetConnection = targetConnectionFactory .createConnection ();
384- Session targetSession = targetConnection .createSession (Session .SESSION_TRANSACTED );
385- Destination targetDestination = createDestination ("target" , targetSession , targetQueue , targetTopic );
386- MessageProducer producer = targetSession .createProducer (targetDestination );
387-
388- if (sourceURL .equals (targetURL ) && sourceDestination .equals (targetDestination )) {
389- context .out .println ("You cannot transfer between " + sourceURL + "/" + sourceDestination + " and " + targetURL + "/" + targetDestination + ".\n " + "That would create an infinite recursion." );
390- throw new IllegalArgumentException ("cannot use " + sourceDestination + " == " + targetDestination );
391- }
392-
393- sourceConnection .start ();
394- int pending = 0 , total = 0 ;
395- while (total < messageCount ) {
396-
397- Message receivedMessage ;
398- if (receiveTimeout < 0 ) {
399- receivedMessage = consumer .receive ();
400- } else if (receiveTimeout == 0 ) {
401- receivedMessage = consumer .receiveNoWait ();
402- } else {
403- receivedMessage = consumer .receive (receiveTimeout );
404380 }
405381
406- if (receivedMessage == null ) {
407- if (isVerbose ()) {
408- context .out .println ("could not receive any more messages" );
409- }
410- break ;
411- }
412- producer .send (receivedMessage );
413- pending ++;
414- total ++;
415-
416- if (isVerbose ()) {
417- context .out .println ("Received message " + total + " with " + pending + " messages pending to be commited" );
418- }
419- if (pending > commitInterval ) {
420- context .out .println ("Transferred " + pending + " messages of " + total );
421- pending = 0 ;
422- targetSession .commit ();
423- if (!isCopy ()) {
424- sourceSession .commit ();
382+ try (MessageConsumer sourceConsumer = consumer ;
383+ ConnectionFactoryClosable targetConnectionFactory = createConnectionFactory ("target" , targetProtocol , targetURL , targetUser , targetPassword , null );
384+ Connection targetConnection = targetConnectionFactory .createConnection ();
385+ Session targetSession = targetConnection .createSession (Session .SESSION_TRANSACTED )) {
386+
387+ Destination targetDestination = createDestination ("target" , targetSession , targetQueue , targetTopic );
388+
389+ try (MessageProducer producer = targetSession .createProducer (targetDestination )) {
390+
391+ if (sourceURL .equals (targetURL ) && sourceDestination .equals (targetDestination )) {
392+ context .out .println ("You cannot transfer between " + sourceURL + "/" + sourceDestination + " and " + targetURL + "/" + targetDestination + ".\n " + "That would create an infinite recursion." );
393+ throw new IllegalArgumentException ("cannot use " + sourceDestination + " == " + targetDestination );
394+ }
395+
396+ sourceConnection .start ();
397+ int pending = 0 , total = 0 ;
398+ while (total < messageCount ) {
399+
400+ Message receivedMessage ;
401+ if (receiveTimeout < 0 ) {
402+ receivedMessage = sourceConsumer .receive ();
403+ } else if (receiveTimeout == 0 ) {
404+ receivedMessage = sourceConsumer .receiveNoWait ();
405+ } else {
406+ receivedMessage = sourceConsumer .receive (receiveTimeout );
407+ }
408+
409+ if (receivedMessage == null ) {
410+ if (isVerbose ()) {
411+ context .out .println ("could not receive any more messages" );
412+ }
413+ break ;
414+ }
415+ producer .send (receivedMessage );
416+ pending ++;
417+ total ++;
418+
419+ if (isVerbose ()) {
420+ context .out .println ("Received message " + total + " with " + pending + " messages pending to be commited" );
421+ }
422+ if (pending > commitInterval ) {
423+ context .out .println ("Transferred " + pending + " messages of " + total );
424+ pending = 0 ;
425+ targetSession .commit ();
426+ if (!isCopy ()) {
427+ sourceSession .commit ();
428+ }
429+ }
430+ }
431+
432+ context .out .println ("Transferred a total of " + total + " messages" );
433+
434+ if (pending != 0 ) {
435+ targetSession .commit ();
436+ if (isCopy ()) {
437+ sourceSession .rollback ();
438+ } else {
439+ sourceSession .commit ();
440+ }
441+ }
442+
443+ return total ;
425444 }
426445 }
427446 }
428-
429- context .out .println ("Transferred a total of " + total + " messages" );
430-
431- if (pending != 0 ) {
432- targetSession .commit ();
433- if (isCopy ()) {
434- sourceSession .rollback ();
435- } else {
436- sourceSession .commit ();
437- }
438- }
439-
440- sourceConnection .close ();
441- sourceConnectionFactory .close ();
442-
443- targetConnection .close ();
444- targetConnectionFactory .close ();
445-
446- return total ;
447447 }
448448
449449 Destination createDestination (String role , Session session , String queue , String topic ) throws Exception {
0 commit comments