Skip to content

Commit 5e9689e

Browse files
committed
iluwatar#2898 Added more unit tests and refactored code
1 parent 7416e1c commit 5e9689e

File tree

12 files changed

+205
-134
lines changed

12 files changed

+205
-134
lines changed

Diff for: publisher-subscriber/README.md renamed to publish-subscribe/README.md

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Publish-Subscribe pattern
2+
title: Publish-Subscribe
33
category: Behavioral
44
language: en
55
tag:
@@ -135,7 +135,7 @@ public class TLender {
135135
LOGGER.error("An error has occurred!", e);
136136
}
137137

138-
TLender tLender = new TLender(topicCFName, topicName);
138+
TLender lender = new TLender(topicCFName, topicName);
139139

140140
System.out.println ("TLender Application Started");
141141
System.out.println ("Press enter to quit application");
@@ -151,11 +151,11 @@ public class TLender {
151151
//Exit if user pressed enter or line is blank
152152
if (line == null || line.trim().length() == 0) {
153153
System.out.println("Exiting...");
154-
tLender.exit();
154+
lender.exit();
155155
}
156156
else { //publish the entered rate
157157
double newRate = Double.parseDouble(line);
158-
tLender.publishRate(newRate);
158+
lender.publishRate(newRate);
159159
}
160160
}
161161
} catch(IOException e) {
@@ -186,15 +186,6 @@ Initial rate is 6.0
186186
Waiting for new rates...
187187
Press enter to quit application
188188

189-
Running the class:
190-
191-
The class must be run after the TLender class is running since TLender spins up the activeMQ broker.
192-
193-
In order to see the messages being sent to multiple subscribers multiple instance of the TBorrower class need to be run. Either run multiple instances in an IDE or execute the following command in a command line from the root folder after generating the target folder:
194-
195-
196-
mvn exec:java -Dexec.mainClass=com.iluwatar.publishersubscriber.TBorrower -Dexec.args="TopicCF RateTopic 6"
197-
198189
```java
199190
public class TBorrower implements MessageListener {
200191

@@ -282,7 +273,7 @@ public class TBorrower implements MessageListener {
282273
System.exit(0);
283274
}
284275

285-
TBorrower tBorrower = new TBorrower(topicCF, topicName, rate);
276+
TBorrower borrower = new TBorrower(topicCF, topicName, rate);
286277

287278
try {
288279
// Run until enter is pressed
@@ -291,7 +282,7 @@ public class TBorrower implements MessageListener {
291282
System.out.println ("TBorrower application started");
292283
System.out.println ("Press enter to quit application");
293284
reader.readLine();
294-
tBorrower.exit();
285+
borrower.exit();
295286
} catch (IOException ioe) {
296287
ioe.printStackTrace();
297288
}
@@ -316,6 +307,7 @@ public class TBorrower implements MessageListener {
316307
```
317308

318309
## Class diagram
310+
![alt text](./etc/publishsubscribe.urm.png "Publish Subscribe class diagram")
319311

320312
## Applicability
321313

Diff for: publish-subscribe/USAGE.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Running the Borrower class:
2+
3+
The class must be run after the TLender class is running since TLender spins up the activeMQ broker.
4+
5+
In order to see the messages being sent to multiple subscribers multiple instance of the TBorrower class need to be run. Either run multiple instances in an IDE or execute the following command in a command line from the root folder after generating the target folder:
6+
7+
8+
mvn exec:java -Dexec.mainClass=com.iluwatar.publishsubscribe.Borrower -Dexec.args="TopicCF RateTopic 6"

Diff for: publisher-subscriber/pom.xml renamed to publish-subscribe/pom.xml

+1-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<artifactId>java-design-patterns</artifactId>
3636
<version>1.26.0-SNAPSHOT</version>
3737
</parent>
38-
<artifactId>publishersubscriber</artifactId>
38+
<artifactId>publishsubscribe</artifactId>
3939

4040
<dependencies>
4141
<dependency>
@@ -58,11 +58,6 @@
5858
<artifactId>xbean-spring</artifactId>
5959
<version>4.24</version>
6060
</dependency>
61-
<dependency>
62-
<groupId>log4j</groupId>
63-
<artifactId>log4j</artifactId>
64-
<version>1.2.17</version>
65-
</dependency>
6661
<dependency>
6762
<groupId>org.apache.activemq.tooling</groupId>
6863
<artifactId>activemq-junit</artifactId>

Diff for: publisher-subscriber/src/main/java/com/iluwatar/publishersubscriber/TBorrower.java renamed to publish-subscribe/src/main/java/com/iluwatar/publishsubscribe/Borrower.java

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.iluwatar.publishersubscriber;
1+
package com.iluwatar.publishsubscribe;
22

33
import lombok.extern.slf4j.Slf4j;
44
import javax.jms.BytesMessage;
@@ -19,14 +19,16 @@
1919
import java.io.InputStreamReader;
2020

2121
@Slf4j
22-
public class TBorrower implements MessageListener {
22+
public class Borrower implements MessageListener {
2323

2424
private TopicConnection tConnection;
2525
private TopicSession tSession;
2626
private Topic topic;
2727
private double currentRate;
28+
private static final String ERROR = "An error has occured!";
29+
private double newRate;
2830

29-
public TBorrower(String topicCFName, String topicName, double initialRate) {
31+
public Borrower(String topicCFName, String topicName, double initialRate) {
3032

3133
currentRate = initialRate;
3234

@@ -50,44 +52,37 @@ public TBorrower(String topicCFName, String topicName, double initialRate) {
5052
tConnection.start();
5153
System.out.println("Initial rate is " + currentRate + " \nWaiting for new rates...");
5254
} catch(NamingException e) {
53-
e.printStackTrace();
54-
LOGGER.error("An error has occurred!", e);
55-
System.exit(1);
55+
LOGGER.error(ERROR, e);
5656
} catch(JMSException e) {
57-
e.printStackTrace();
58-
LOGGER.error("An error has occurred!", e);
59-
System.exit(1);
57+
LOGGER.error(ERROR, e);
6058
}
6159
}
6260

6361
//This method is called asynchronously by the activeMQ broker
6462
public void onMessage(Message message) {
65-
6663
try {
6764
BytesMessage bMessage = (BytesMessage) message;
68-
double newRate = ((BytesMessage) bMessage).readDouble();
65+
double newRate = bMessage.readDouble();
66+
setNewRate(newRate);
6967

7068
if (currentRate - newRate >= 1)
7169
System.out.println("New Rate is " + newRate + " - Consider refinancing");
7270
else
7371
System.out.println("New Rate is " + newRate + " - Consider keeping current rate");
7472
} catch(JMSException e) {
75-
e.printStackTrace();
76-
LOGGER.error("An error occurred!", e);
77-
System.exit(1);
73+
LOGGER.error(ERROR, e);
7874
}
7975
System.out.println("Waiting for new rates...");
8076
}
8177

82-
private void exit() {
78+
public boolean close() {
8379
try {
8480
tConnection.close();
81+
return true;
8582
} catch(JMSException e) {
86-
e.printStackTrace();
87-
LOGGER.error("An error has occurred!", e);
88-
System.exit(1);
83+
LOGGER.error(ERROR, e);
84+
return false;
8985
}
90-
System.exit(0);
9186
}
9287

9388
public static void main(String[] args) {
@@ -105,7 +100,7 @@ public static void main(String[] args) {
105100
System.exit(0);
106101
}
107102

108-
TBorrower tBorrower = new TBorrower(topicCF, topicName, rate);
103+
Borrower borrower = new Borrower(topicCF, topicName, rate);
109104

110105
try {
111106
// Run until enter is pressed
@@ -114,9 +109,10 @@ public static void main(String[] args) {
114109
System.out.println ("TBorrower application started");
115110
System.out.println ("Press enter to quit application");
116111
reader.readLine();
117-
tBorrower.exit();
118-
} catch (IOException ioe) {
119-
ioe.printStackTrace();
112+
borrower.close();
113+
System.exit(0);
114+
} catch (IOException e) {
115+
LOGGER.error(ERROR, e);
120116
}
121117
}
122118

@@ -135,4 +131,8 @@ public Topic getTopic() {
135131
public double getCurrentRate() {
136132
return currentRate;
137133
}
134+
135+
public double getNewRate() { return newRate; }
136+
137+
private void setNewRate(double newRate) { this.newRate = newRate; }
138138
}

Diff for: publisher-subscriber/src/main/java/com/iluwatar/publishersubscriber/TLender.java renamed to publish-subscribe/src/main/java/com/iluwatar/publishsubscribe/Lender.java

+37-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.iluwatar.publishersubscriber;
1+
package com.iluwatar.publishsubscribe;
22

33
import lombok.extern.slf4j.Slf4j;
44
import org.apache.activemq.broker.BrokerService;
@@ -14,18 +14,23 @@
1414
import javax.naming.InitialContext;
1515
import javax.naming.NamingException;
1616
import java.io.BufferedReader;
17+
import java.io.FileInputStream;
18+
import java.io.FileNotFoundException;
1719
import java.io.IOException;
20+
import java.io.InputStream;
1821
import java.io.InputStreamReader;
22+
import java.util.Properties;
1923

2024
@Slf4j
21-
public class TLender {
25+
public class Lender {
2226

2327
private TopicConnection tConnection;
2428
private TopicSession tSession;
2529
private Topic topic;
2630
private TopicPublisher publisher;
31+
private static final String ERROR = "An error has occured!";
2732

28-
public TLender(String topicCFName, String topicName) {
33+
public Lender(String topicCFName, String topicName) {
2934

3035
try {
3136
//create context and retrieve objects from directory
@@ -44,17 +49,13 @@ public TLender(String topicCFName, String topicName) {
4449

4550
tConnection.start();
4651
} catch(NamingException e) {
47-
e.printStackTrace();
48-
LOGGER.error("An error has occurred!", e);
49-
System.exit(1);
52+
LOGGER.error(ERROR, e);
5053
} catch(JMSException e) {
51-
e.printStackTrace();
52-
LOGGER.error("An error has occurred!", e);
53-
System.exit(1);
54+
LOGGER.error(ERROR, e);
5455
}
5556
}
5657

57-
private void publishRate(double newRate) {
58+
public void publishRate(double newRate) {
5859

5960
try {
6061
//create JMS message
@@ -64,21 +65,18 @@ private void publishRate(double newRate) {
6465
//publish message
6566
publisher.publish(message);
6667
} catch(JMSException e) {
67-
e.printStackTrace();
68-
LOGGER.error("An error has occurred!", e);
69-
System.exit(1);
68+
LOGGER.error(ERROR, e);
7069
}
7170
}
7271

73-
private void exit() {
72+
public boolean close() {
7473
try {
7574
tConnection.close();
75+
return true;
7676
} catch(JMSException e) {
77-
e.printStackTrace();
78-
LOGGER.error("An error has occurred!", e);
79-
System.exit(1);
77+
LOGGER.error(ERROR, e);
78+
return false;
8079
}
81-
System.exit(0);
8280
}
8381

8482
public static void main(String[] args) {
@@ -90,26 +88,35 @@ public static void main(String[] args) {
9088
topicCFName = args[0];
9189
topicName = args[1];
9290
} else {
93-
System.out.println("Invalid arguments. Should be: ");
94-
System.out.println("java TLender [factory] [topic]");
91+
LOGGER.info("Invalid arguments. Should be: ");
92+
LOGGER.info("java TLender [factory] [topic]");
9593
System.exit(1);
9694
}
9795

9896
try {
97+
//Get configuration properties
98+
Properties props = new Properties();
99+
InputStream in = new FileInputStream("publish-subscribe/src/main/resources/config.properties");
100+
props.load(in);
101+
in.close();
102+
99103
// Create and start activeMQ broker. Broker decouples publishers and subscribers.
100-
//Additionally brokers manage threads and asynchronous sending and receiving of messages.
104+
// Additionally brokers manage threads and asynchronous sending and receiving of messages.
101105
BrokerService broker = new BrokerService();
102-
broker.addConnector("tcp://localhost:61616");
106+
broker.addConnector(props.getProperty("ADDRESS"));
103107
broker.start();
104108

109+
} catch(FileNotFoundException e) {
110+
LOGGER.error(ERROR, e);
111+
} catch(IOException e) {
112+
LOGGER.error(ERROR, e);
105113
} catch(Exception e) {
106-
e.printStackTrace();
107-
LOGGER.error("An error has occurred!", e);
114+
LOGGER.error(ERROR, e);
108115
}
109116

110-
TLender tLender = new TLender(topicCFName, topicName);
117+
Lender lender = new Lender(topicCFName, topicName);
111118

112-
System.out.println ("TLender Application Started");
119+
LOGGER.info("TLender Application Started");
113120
System.out.println ("Press enter to quit application");
114121
System.out.println ("Enter: Rate");
115122
System.out.println("\ne.g. 6.8");
@@ -123,16 +130,16 @@ public static void main(String[] args) {
123130
//Exit if user pressed enter or line is blank
124131
if (line == null || line.trim().length() == 0) {
125132
System.out.println("Exiting...");
126-
tLender.exit();
133+
lender.close();
134+
System.exit(0);
127135
}
128136
else { //publish the entered rate
129137
double newRate = Double.parseDouble(line);
130-
tLender.publishRate(newRate);
138+
lender.publishRate(newRate);
131139
}
132140
}
133141
} catch(IOException e) {
134-
e.printStackTrace();
135-
LOGGER.error("An error has occurred!", e);
142+
LOGGER.error(ERROR, e);
136143
}
137144
}
138145

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADDRESS=tcp://localhost:61616

0 commit comments

Comments
 (0)