Skip to content

Commit ea90dd4

Browse files
committed
Fixing checkstyle violations
1 parent 95fdb99 commit ea90dd4

File tree

6 files changed

+289
-245
lines changed

6 files changed

+289
-245
lines changed

publish-subsribe/src/main/java/com/iluwatar/publishsubscribe/App.java

Lines changed: 125 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,142 @@
33
import com.iluwatar.publishsubscribe.jms.JmsUtil;
44
import com.iluwatar.publishsubscribe.model.Message;
55
import com.iluwatar.publishsubscribe.publisher.TopicPublisher;
6-
import com.iluwatar.publishsubscribe.subscriber.TopicSubscriber;
76
import com.iluwatar.publishsubscribe.subscriber.SubscriberType;
8-
7+
import com.iluwatar.publishsubscribe.subscriber.TopicSubscriber;
98
import java.util.ArrayList;
109
import java.util.List;
1110

1211
/**
13-
* Main application demonstrating different aspects of JMS publish-subscribe
14-
* pattern.
12+
* Main application demonstrating different aspects of JMS publish-subscribe pattern.
1513
*/
1614
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);
3534
}
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");
6060
}
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");
9898
}
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");
125125
}
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+
}
141138
}
139+
}
140+
} catch (Exception e) {
141+
System.err.println("Error during subscriber cleanup: " + e.getMessage());
142142
}
143+
}
143144
}

publish-subsribe/src/main/java/com/iluwatar/publishsubscribe/jms/JmsUtil.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.iluwatar.publishsubscribe.jms;
22

3-
import org.apache.activemq.ActiveMQConnectionFactory;
4-
import org.apache.activemq.broker.BrokerService;
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
55
import javax.jms.Connection;
66
import javax.jms.ConnectionFactory;
77
import javax.jms.JMSException;
88
import javax.jms.Session;
9-
import java.util.Map;
10-
import java.util.concurrent.ConcurrentHashMap;
9+
import org.apache.activemq.ActiveMQConnectionFactory;
10+
import org.apache.activemq.broker.BrokerService;
1111

1212
/**
13-
* JMS utility class that manages connections and provides an embedded message broker.
14-
* Supports both shared connections and client-specific connections for durable subscriptions.
13+
* JMS utility class that manages connections and provides an embedded message
14+
* broker.
15+
* Supports both shared connections and client-specific connections for durable
16+
* subscriptions.
1517
*/
1618
public final class JmsUtil {
1719
private static final String BROKER_URL = "tcp://localhost:61616";
@@ -21,10 +23,21 @@ public final class JmsUtil {
2123
private static Map<String, Connection> clientConnections = new ConcurrentHashMap<>();
2224
private static boolean isInitialized = false;
2325

26+
/**
27+
* Private constructor to prevent instantiation.
28+
*/
2429
private JmsUtil() {
2530
// Utility class, prevent instantiation
2631
}
2732

33+
/**
34+
* Initializes the JMS environment by starting the embedded broker and creating
35+
* the default connection. This method is thread-safe and ensures single
36+
* initialization.
37+
*
38+
*
39+
* @throws RuntimeException if initialization fails
40+
*/
2841
public static synchronized void initialize() {
2942
if (!isInitialized) {
3043
try {
@@ -46,7 +59,8 @@ public static synchronized void initialize() {
4659

4760
/**
4861
* Creates a JMS session, optionally with a client ID for durable subscriptions.
49-
* Each client ID gets its own dedicated connection to support durable subscribers.
62+
* Each client ID gets its own dedicated connection to support durable
63+
* subscribers.
5064
*/
5165
public static Session createSession(String clientId) throws JMSException {
5266
if (!isInitialized) {
@@ -70,6 +84,9 @@ public static Session createSession(String clientId) throws JMSException {
7084
return conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
7185
}
7286

87+
/**
88+
* Creates a default JMS session without client ID.
89+
*/
7390
public static Session createSession() throws JMSException {
7491
return createSession(null);
7592
}
@@ -97,6 +114,10 @@ public static synchronized void closeConnection() {
97114
}
98115
}
99116

117+
/**
118+
* Resets the JMS environment by closing existing connections and
119+
* reinitializing.
120+
*/
100121
public static synchronized void reset() {
101122
closeConnection();
102123
isInitialized = false;

publish-subsribe/src/main/java/com/iluwatar/publishsubscribe/model/Message.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22

33
import java.io.Serializable;
44

5+
/**
6+
* Represents a message in the publish-subscribe system.
7+
*/
58
public class Message implements Serializable {
6-
private String content;
7-
private String topic;
9+
private String content;
10+
private String topic;
811

9-
public Message(String content, String topic) {
10-
this.content = content;
11-
this.topic = topic;
12-
}
12+
public Message(String content, String topic) {
13+
this.content = content;
14+
this.topic = topic;
15+
}
1316

14-
public String getContent() {
15-
return content;
16-
}
17+
public String getContent() {
18+
return content;
19+
}
1720

18-
public String getTopic() {
19-
return topic;
20-
}
21+
public String getTopic() {
22+
return topic;
23+
}
2124

22-
@Override
23-
public String toString() {
24-
return "Message{topic='" + topic + "', content='" + content + "'}";
25-
}
25+
@Override
26+
public String toString() {
27+
return "Message{topic='" + topic + "', content='" + content + "'}";
28+
}
2629
}

0 commit comments

Comments
 (0)