1010import java .util .List ;
1111
1212/**
13- * Main application demonstrating different aspects of JMS publish-subscribe pattern.
13+ * Main application demonstrating different aspects of JMS publish-subscribe
14+ * pattern.
1415 */
1516public final class App {
1617 private static TopicPublisher publisher ;
1718
1819 public static void main (String [] args ) {
1920 try {
2021 publisher = new TopicPublisher ("NEWS" );
21-
22+
2223 // Run each demonstration independently
2324 demonstrateBasicPubSub ();
2425 demonstrateDurableSubscriptions ();
@@ -30,6 +31,7 @@ public static void main(String[] args) {
3031 } finally {
3132 cleanup (null );
3233 }
34+ JmsUtil .closeConnection ();
3335 }
3436
3537 /**
@@ -39,17 +41,17 @@ public static void main(String[] args) {
3941 private static void demonstrateBasicPubSub () throws Exception {
4042 System .out .println ("\n === Basic Publish-Subscribe Demonstration ===" );
4143 List <TopicSubscriber > subscribers = new ArrayList <>();
42-
44+
4345 try {
4446 // Create basic subscribers
4547 subscribers .add (new TopicSubscriber ("BasicSub1" , "NEWS" , SubscriberType .NONDURABLE , null ));
4648 subscribers .add (new TopicSubscriber ("BasicSub2" , "NEWS" , SubscriberType .NONDURABLE , null ));
4749 Thread .sleep (100 ); // Wait for subscribers to initialize
48-
50+
4951 // Publish messages - all subscribers should receive all messages
5052 publisher .publish (new Message ("Basic message 1" , "NEWS" ));
5153 publisher .publish (new Message ("Basic message 2" , "NEWS" ));
52-
54+
5355 Thread .sleep (1000 ); // Wait for message processing
5456 } finally {
5557 cleanup (subscribers );
@@ -58,61 +60,63 @@ private static void demonstrateBasicPubSub() throws Exception {
5860 }
5961
6062 /**
61- * Demonstrates durable subscriptions that persist messages when subscribers are offline.
63+ * Demonstrates durable subscriptions that persist messages when subscribers are
64+ * offline.
6265 */
6366 private static void demonstrateDurableSubscriptions () throws Exception {
6467 System .out .println ("\n === Durable Subscriptions Demonstration ===" );
6568 List <TopicSubscriber > subscribers = new ArrayList <>();
66-
69+
6770 try {
6871 // Create durable subscriber
69- TopicSubscriber durableSub = new TopicSubscriber ("DurableSub" , "NEWS" ,
70- SubscriberType .DURABLE , "durable-client" );
72+ TopicSubscriber durableSub = new TopicSubscriber ("DurableSub" , "NEWS" ,
73+ SubscriberType .DURABLE , "durable-client" );
7174 subscribers .add (durableSub );
7275 Thread .sleep (100 );
73-
76+
7477 // First message - subscriber is online
7578 publisher .publish (new Message ("Durable message while online" , "NEWS" ));
7679 Thread .sleep (500 );
77-
80+
7881 // Disconnect subscriber
7982 durableSub .close ();
8083 subscribers .clear ();
81-
84+
8285 // Send message while subscriber is offline
8386 publisher .publish (new Message ("Durable message while offline" , "NEWS" ));
8487 Thread .sleep (500 );
85-
88+
8689 // Reconnect subscriber - should receive offline message
87- subscribers .add (new TopicSubscriber ("DurableSub" , "NEWS" ,
88- SubscriberType .DURABLE , "durable-client" ));
90+ subscribers .add (new TopicSubscriber ("DurableSub" , "NEWS" ,
91+ SubscriberType .DURABLE , "durable-client" ));
8992 Thread .sleep (1000 );
90-
93+
9194 } finally {
9295 cleanup (subscribers );
9396 System .out .println ("=== Durable Demonstration Completed ===\n " );
9497 }
9598 }
9699
97100 /**
98- * Demonstrates shared subscriptions where messages are distributed among subscribers.
101+ * Demonstrates shared subscriptions where messages are distributed among
102+ * subscribers.
99103 */
100104 private static void demonstrateSharedSubscriptions () throws Exception {
101105 System .out .println ("\n === Shared Subscriptions Demonstration ===" );
102106 List <TopicSubscriber > subscribers = new ArrayList <>();
103-
107+
104108 try {
105109 // Create shared subscribers
106110 subscribers .add (new TopicSubscriber ("SharedSub1" , "NEWS" , SubscriberType .SHARED , null ));
107111 subscribers .add (new TopicSubscriber ("SharedSub2" , "NEWS" , SubscriberType .SHARED , null ));
108112 Thread .sleep (100 );
109-
113+
110114 // Messages should be distributed between subscribers
111115 for (int i = 1 ; i <= 4 ; i ++) {
112116 publisher .publish (new Message ("Shared message " + i , "NEWS" ));
113117 Thread .sleep (100 );
114118 }
115-
119+
116120 Thread .sleep (1000 );
117121 } finally {
118122 cleanup (subscribers );
0 commit comments