|
3 | 3 | import com.iluwatar.publishsubscribe.jms.JmsUtil; |
4 | 4 | import com.iluwatar.publishsubscribe.model.Message; |
5 | 5 | import com.iluwatar.publishsubscribe.publisher.TopicPublisher; |
6 | | -import com.iluwatar.publishsubscribe.subscriber.TopicSubscriber; |
7 | 6 | import com.iluwatar.publishsubscribe.subscriber.SubscriberType; |
8 | | - |
| 7 | +import com.iluwatar.publishsubscribe.subscriber.TopicSubscriber; |
9 | 8 | import java.util.ArrayList; |
10 | 9 | import java.util.List; |
11 | 10 |
|
12 | 11 | /** |
13 | | - * Main application demonstrating different aspects of JMS publish-subscribe |
14 | | - * pattern. |
| 12 | + * Main application demonstrating different aspects of JMS publish-subscribe pattern. |
15 | 13 | */ |
16 | 14 | public final class App { |
17 | | - private static TopicPublisher publisher; |
18 | | - |
19 | | - public static void main(String[] args) { |
20 | | - try { |
21 | | - publisher = new TopicPublisher("NEWS"); |
22 | | - |
23 | | - // Run each demonstration independently |
24 | | - demonstrateBasicPubSub(); |
25 | | - demonstrateDurableSubscriptions(); |
26 | | - demonstrateSharedSubscriptions(); |
27 | | - |
28 | | - } catch (Exception e) { |
29 | | - System.err.println("Error in publish-subscribe demo: " + e.getMessage()); |
30 | | - e.printStackTrace(); |
31 | | - } finally { |
32 | | - cleanup(null); |
33 | | - } |
34 | | - JmsUtil.closeConnection(); |
| 15 | + private static TopicPublisher publisher; |
| 16 | + |
| 17 | + private App() { |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Main method to run the demo. |
| 22 | + */ |
| 23 | + public static void main(String[] args) { |
| 24 | + try { |
| 25 | + publisher = new TopicPublisher("NEWS"); |
| 26 | + demonstrateBasicPubSub(); |
| 27 | + demonstrateDurableSubscriptions(); |
| 28 | + demonstrateSharedSubscriptions(); |
| 29 | + } catch (Exception e) { |
| 30 | + System.err.println("Error in publish-subscribe demo: " + e.getMessage()); |
| 31 | + e.printStackTrace(); |
| 32 | + } finally { |
| 33 | + cleanup(null); |
35 | 34 | } |
36 | | - |
37 | | - /** |
38 | | - * Demonstrates basic publish-subscribe with non-durable subscribers. |
39 | | - * All subscribers receive all messages. |
40 | | - */ |
41 | | - private static void demonstrateBasicPubSub() throws Exception { |
42 | | - System.out.println("\n=== Basic Publish-Subscribe Demonstration ==="); |
43 | | - List<TopicSubscriber> subscribers = new ArrayList<>(); |
44 | | - |
45 | | - try { |
46 | | - // Create basic subscribers |
47 | | - subscribers.add(new TopicSubscriber("BasicSub1", "NEWS", SubscriberType.NONDURABLE, null)); |
48 | | - subscribers.add(new TopicSubscriber("BasicSub2", "NEWS", SubscriberType.NONDURABLE, null)); |
49 | | - Thread.sleep(100); // Wait for subscribers to initialize |
50 | | - |
51 | | - // Publish messages - all subscribers should receive all messages |
52 | | - publisher.publish(new Message("Basic message 1", "NEWS")); |
53 | | - publisher.publish(new Message("Basic message 2", "NEWS")); |
54 | | - |
55 | | - Thread.sleep(1000); // Wait for message processing |
56 | | - } finally { |
57 | | - cleanup(subscribers); |
58 | | - System.out.println("=== Basic Demonstration Completed ===\n"); |
59 | | - } |
| 35 | + JmsUtil.closeConnection(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Demonstrates basic publish-subscribe with non-durable subscribers. |
| 40 | + * All subscribers receive all messages. |
| 41 | + */ |
| 42 | + private static void demonstrateBasicPubSub() throws Exception { |
| 43 | + System.out.println("\n=== Basic Publish-Subscribe Demonstration ==="); |
| 44 | + List<TopicSubscriber> subscribers = new ArrayList<>(); |
| 45 | + |
| 46 | + try { |
| 47 | + // Create basic subscribers |
| 48 | + subscribers.add(new TopicSubscriber("BasicSub1", "NEWS", SubscriberType.NONDURABLE, null)); |
| 49 | + subscribers.add(new TopicSubscriber("BasicSub2", "NEWS", SubscriberType.NONDURABLE, null)); |
| 50 | + Thread.sleep(100); // Wait for subscribers to initialize |
| 51 | + |
| 52 | + // Publish messages - all subscribers should receive all messages |
| 53 | + publisher.publish(new Message("Basic message 1", "NEWS")); |
| 54 | + publisher.publish(new Message("Basic message 2", "NEWS")); |
| 55 | + |
| 56 | + Thread.sleep(1000); // Wait for message processing |
| 57 | + } finally { |
| 58 | + cleanup(subscribers); |
| 59 | + System.out.println("=== Basic Demonstration Completed ===\n"); |
60 | 60 | } |
61 | | - |
62 | | - /** |
63 | | - * Demonstrates durable subscriptions that persist messages when subscribers are |
64 | | - * offline. |
65 | | - */ |
66 | | - private static void demonstrateDurableSubscriptions() throws Exception { |
67 | | - System.out.println("\n=== Durable Subscriptions Demonstration ==="); |
68 | | - List<TopicSubscriber> subscribers = new ArrayList<>(); |
69 | | - |
70 | | - try { |
71 | | - // Create durable subscriber |
72 | | - TopicSubscriber durableSub = new TopicSubscriber("DurableSub", "NEWS", |
73 | | - SubscriberType.DURABLE, "durable-client"); |
74 | | - subscribers.add(durableSub); |
75 | | - Thread.sleep(100); |
76 | | - |
77 | | - // First message - subscriber is online |
78 | | - publisher.publish(new Message("Durable message while online", "NEWS")); |
79 | | - Thread.sleep(500); |
80 | | - |
81 | | - // Disconnect subscriber |
82 | | - durableSub.close(); |
83 | | - subscribers.clear(); |
84 | | - |
85 | | - // Send message while subscriber is offline |
86 | | - publisher.publish(new Message("Durable message while offline", "NEWS")); |
87 | | - Thread.sleep(500); |
88 | | - |
89 | | - // Reconnect subscriber - should receive offline message |
90 | | - subscribers.add(new TopicSubscriber("DurableSub", "NEWS", |
91 | | - SubscriberType.DURABLE, "durable-client")); |
92 | | - Thread.sleep(1000); |
93 | | - |
94 | | - } finally { |
95 | | - cleanup(subscribers); |
96 | | - System.out.println("=== Durable Demonstration Completed ===\n"); |
97 | | - } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Demonstrates durable subscriptions that persist messages when subscribers are |
| 65 | + * offline. |
| 66 | + */ |
| 67 | + private static void demonstrateDurableSubscriptions() throws Exception { |
| 68 | + System.out.println("\n=== Durable Subscriptions Demonstration ==="); |
| 69 | + List<TopicSubscriber> subscribers = new ArrayList<>(); |
| 70 | + |
| 71 | + try { |
| 72 | + // Create durable subscriber |
| 73 | + TopicSubscriber durableSub = new TopicSubscriber("DurableSub", "NEWS", |
| 74 | + SubscriberType.DURABLE, "durable-client"); |
| 75 | + subscribers.add(durableSub); |
| 76 | + Thread.sleep(100); |
| 77 | + |
| 78 | + // First message - subscriber is online |
| 79 | + publisher.publish(new Message("Durable message while online", "NEWS")); |
| 80 | + Thread.sleep(500); |
| 81 | + |
| 82 | + // Disconnect subscriber |
| 83 | + durableSub.close(); |
| 84 | + subscribers.clear(); |
| 85 | + |
| 86 | + // Send message while subscriber is offline |
| 87 | + publisher.publish(new Message("Durable message while offline", "NEWS")); |
| 88 | + Thread.sleep(500); |
| 89 | + |
| 90 | + // Reconnect subscriber - should receive offline message |
| 91 | + subscribers.add(new TopicSubscriber("DurableSub", "NEWS", |
| 92 | + SubscriberType.DURABLE, "durable-client")); |
| 93 | + Thread.sleep(1000); |
| 94 | + |
| 95 | + } finally { |
| 96 | + cleanup(subscribers); |
| 97 | + System.out.println("=== Durable Demonstration Completed ===\n"); |
98 | 98 | } |
99 | | - |
100 | | - /** |
101 | | - * Demonstrates shared subscriptions where messages are distributed among |
102 | | - * subscribers. |
103 | | - */ |
104 | | - private static void demonstrateSharedSubscriptions() throws Exception { |
105 | | - System.out.println("\n=== Shared Subscriptions Demonstration ==="); |
106 | | - List<TopicSubscriber> subscribers = new ArrayList<>(); |
107 | | - |
108 | | - try { |
109 | | - // Create shared subscribers |
110 | | - subscribers.add(new TopicSubscriber("SharedSub1", "NEWS", SubscriberType.SHARED, null)); |
111 | | - subscribers.add(new TopicSubscriber("SharedSub2", "NEWS", SubscriberType.SHARED, null)); |
112 | | - Thread.sleep(100); |
113 | | - |
114 | | - // Messages should be distributed between subscribers |
115 | | - for (int i = 1; i <= 4; i++) { |
116 | | - publisher.publish(new Message("Shared message " + i, "NEWS")); |
117 | | - Thread.sleep(100); |
118 | | - } |
119 | | - |
120 | | - Thread.sleep(1000); |
121 | | - } finally { |
122 | | - cleanup(subscribers); |
123 | | - System.out.println("=== Shared Demonstration Completed ===\n"); |
124 | | - } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Demonstrates shared subscriptions where messages are distributed among |
| 103 | + * subscribers. |
| 104 | + */ |
| 105 | + private static void demonstrateSharedSubscriptions() throws Exception { |
| 106 | + System.out.println("\n=== Shared Subscriptions Demonstration ==="); |
| 107 | + List<TopicSubscriber> subscribers = new ArrayList<>(); |
| 108 | + |
| 109 | + try { |
| 110 | + // Create shared subscribers |
| 111 | + subscribers.add(new TopicSubscriber("SharedSub1", "NEWS", SubscriberType.SHARED, null)); |
| 112 | + subscribers.add(new TopicSubscriber("SharedSub2", "NEWS", SubscriberType.SHARED, null)); |
| 113 | + Thread.sleep(100); |
| 114 | + |
| 115 | + // Messages should be distributed between subscribers |
| 116 | + for (int i = 1; i <= 4; i++) { |
| 117 | + publisher.publish(new Message("Shared message " + i, "NEWS")); |
| 118 | + Thread.sleep(100); |
| 119 | + } |
| 120 | + |
| 121 | + Thread.sleep(1000); |
| 122 | + } finally { |
| 123 | + cleanup(subscribers); |
| 124 | + System.out.println("=== Shared Demonstration Completed ===\n"); |
125 | 125 | } |
126 | | - |
127 | | - /** |
128 | | - * Cleanup specified subscribers and optionally the publisher. |
129 | | - */ |
130 | | - private static void cleanup(List<TopicSubscriber> subscribers) { |
131 | | - try { |
132 | | - if (subscribers != null) { |
133 | | - for (TopicSubscriber subscriber : subscribers) { |
134 | | - if (subscriber != null) { |
135 | | - subscriber.close(); |
136 | | - } |
137 | | - } |
138 | | - } |
139 | | - } catch (Exception e) { |
140 | | - System.err.println("Error during subscriber cleanup: " + e.getMessage()); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Cleanup specified subscribers and optionally the publisher. |
| 130 | + */ |
| 131 | + private static void cleanup(List<TopicSubscriber> subscribers) { |
| 132 | + try { |
| 133 | + if (subscribers != null) { |
| 134 | + for (TopicSubscriber subscriber : subscribers) { |
| 135 | + if (subscriber != null) { |
| 136 | + subscriber.close(); |
| 137 | + } |
141 | 138 | } |
| 139 | + } |
| 140 | + } catch (Exception e) { |
| 141 | + System.err.println("Error during subscriber cleanup: " + e.getMessage()); |
142 | 142 | } |
| 143 | + } |
143 | 144 | } |
0 commit comments