Skip to content

Commit 300ec41

Browse files
authored
make it start and stoppable (#883)
Signed-off-by: Mark Nelson <mark.x.nelson@oracle.com>
1 parent 05bffc6 commit 300ec41

File tree

4 files changed

+160
-75
lines changed

4 files changed

+160
-75
lines changed

tolldemo/toll-reader/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<groupId>org.springframework.kafka</groupId>
6262
<artifactId>spring-kafka</artifactId>
6363
</dependency> -->
64+
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
</dependency>
6469
</dependencies>
6570

6671
<build>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.tollreader;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ConfigurableApplicationContext;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/")
12+
public class Controller {
13+
14+
@Autowired
15+
private ConfigurableApplicationContext context;
16+
17+
public Controller(ConfigurableApplicationContext context) {
18+
this.context = context;
19+
}
20+
21+
// yes i know how ugly this is :)
22+
@GetMapping("/start/{delay}")
23+
public String start(@PathVariable("delay") int delay) {
24+
MessageTaskExecutor mte = (MessageTaskExecutor) context.getBean("messageTaskExecutor");
25+
mte.setDelay(delay);
26+
mte.start();
27+
return "started\n";
28+
}
29+
30+
@GetMapping("/stop")
31+
public String stop() {
32+
MessageTaskExecutor mte = (MessageTaskExecutor) context.getBean("messageTaskExecutor");
33+
mte.stop();
34+
return "stopped\n";
35+
}
36+
37+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.example.tollreader;
2+
3+
import java.security.SecureRandom;
4+
import java.time.LocalDateTime;
5+
import java.time.format.DateTimeFormatter;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.ApplicationListener;
9+
import org.springframework.context.Lifecycle;
10+
import org.springframework.core.task.TaskExecutor;
11+
import org.springframework.jms.core.JmsTemplate;
12+
import org.springframework.jms.core.MessageCreator;
13+
import org.springframework.stereotype.Component;
14+
15+
import jakarta.jms.JMSException;
16+
import jakarta.jms.Message;
17+
import jakarta.jms.Session;
18+
import jakarta.json.Json;
19+
import jakarta.json.JsonObject;
20+
import lombok.extern.slf4j.Slf4j;
21+
22+
@Component
23+
@Slf4j
24+
public class MessageTaskExecutor implements Lifecycle {
25+
26+
private TaskExecutor taskExecutor;
27+
private boolean running = false;
28+
private int delay = 1000;
29+
30+
public MessageTaskExecutor(TaskExecutor taskExecutor) {
31+
this.taskExecutor = taskExecutor;
32+
}
33+
34+
public void start() {
35+
running = true;
36+
log.info("started sending messages");
37+
taskExecutor.execute(new MessageSenderTask(delay));
38+
}
39+
40+
public void stop() {
41+
running = false;
42+
log.info("stopped sending messages");
43+
}
44+
45+
public boolean isRunning() {
46+
return running;
47+
}
48+
49+
public void setDelay(int delay) {
50+
this.delay = delay;
51+
}
52+
53+
private class MessageSenderTask implements Runnable {
54+
private int delay = 1000;
55+
private static final SecureRandom random = new SecureRandom();
56+
private static final Integer minNumber = 10000;
57+
private static final Integer maxNumber = 99999;
58+
59+
public MessageSenderTask(int delay) {
60+
this.delay = delay;
61+
}
62+
63+
@Autowired
64+
private JmsTemplate jmsTemplate;
65+
66+
private static <T extends Enum<?>> T randomEnum(Class<T> clazz) {
67+
int x = random.nextInt(clazz.getEnumConstants().length);
68+
return clazz.getEnumConstants()[x];
69+
}
70+
71+
// Why supresswarnings? -- it's your ide making it required
72+
@SuppressWarnings("null")
73+
private void sendMessage(JsonObject tolldata) {
74+
jmsTemplate.send("TollGate", new MessageCreator() {
75+
@Override
76+
public Message createMessage(Session session) throws JMSException {
77+
return session.createTextMessage(tolldata.toString());
78+
}
79+
});
80+
}
81+
82+
public void sendMessage() throws Exception {
83+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
84+
LocalDateTime now = LocalDateTime.now();
85+
String dateTimeString = now.format(formatter);
86+
87+
int licNumber = random.nextInt(maxNumber - minNumber) + minNumber;
88+
int tagId = random.nextInt(maxNumber - minNumber) + minNumber;
89+
int accountNumber = random.nextInt(maxNumber - minNumber) + minNumber;
90+
String state = randomEnum(State.class).toString();
91+
String carType = randomEnum(CarType.class).toString();
92+
93+
JsonObject data = Json.createObjectBuilder()
94+
.add("accountnumber", accountNumber) // This could be looked up in the DB from the tagId?
95+
.add("license-plate", state + "-" + Integer.toString(licNumber)) // This could be looked up in the DB from the tagId?
96+
.add("cartype", carType) // This could be looked up in the DB from the tagId?
97+
.add("tagid", tagId)
98+
.add("timestamp", dateTimeString)
99+
.build();
100+
101+
log.info("Toll Data :" + data.toString());
102+
sendMessage(data);
103+
}
104+
105+
106+
public void run() {
107+
while (isRunning()) {
108+
try {
109+
// wait first so we always wait even if sendMessage() fails
110+
Thread.sleep(delay);
111+
sendMessage();
112+
} catch (Exception ignore) {}
113+
}
114+
}
115+
}
116+
117+
}

tolldemo/toll-reader/src/main/java/com/example/tollreader/TollreaderApplication.java

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,12 @@
11
package com.example.tollreader;
22

3-
import jakarta.jms.JMSException;
4-
import jakarta.jms.Message;
5-
import jakarta.jms.Session;
6-
import jakarta.json.Json;
7-
import jakarta.json.JsonObject;
8-
import lombok.extern.slf4j.Slf4j;
9-
10-
import java.time.format.DateTimeFormatter;
11-
import java.time.LocalDateTime;
12-
import java.security.SecureRandom;
13-
14-
import org.springframework.beans.factory.annotation.Autowired;
15-
import org.springframework.boot.CommandLineRunner;
163
import org.springframework.boot.SpringApplication;
174
import org.springframework.boot.autoconfigure.SpringBootApplication;
185
import org.springframework.jms.annotation.EnableJms;
19-
import org.springframework.jms.core.JmsTemplate;
20-
import org.springframework.jms.core.MessageCreator;
216

227
@EnableJms
238
@SpringBootApplication
24-
@Slf4j
25-
public class TollreaderApplication implements CommandLineRunner {
26-
27-
private static final SecureRandom random = new SecureRandom();
28-
private static final Integer minNumber = 10000;
29-
private static final Integer maxNumber = 99999;
30-
31-
@Autowired
32-
private JmsTemplate jmsTemplate;
33-
34-
private static <T extends Enum<?>> T randomEnum(Class<T> clazz) {
35-
int x = random.nextInt(clazz.getEnumConstants().length);
36-
return clazz.getEnumConstants()[x];
37-
}
38-
39-
// Why supresswarnings?
40-
@SuppressWarnings("null")
41-
private void sendMessage(JsonObject tolldata) {
42-
jmsTemplate.send("TollGate", new MessageCreator() {
43-
@Override
44-
public Message createMessage(Session session) throws JMSException {
45-
return session.createTextMessage(tolldata.toString());
46-
}
47-
48-
});
49-
}
50-
51-
@Override
52-
public void run(String... args) throws Exception {
53-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
54-
LocalDateTime now = LocalDateTime.now();
55-
String dateTimeString = now.format(formatter);
56-
57-
int sleepTime = 1000;
58-
if (args.length > 0 && !args[0].isBlank()) {
59-
sleepTime = Integer.parseInt(args[0]);
60-
}
61-
62-
log.info("Sleeptime :" + Integer.toString(sleepTime));
63-
64-
while (true) {
65-
int licNumber = random.nextInt(maxNumber - minNumber) + minNumber;
66-
int tagId = random.nextInt(maxNumber - minNumber) + minNumber;
67-
int accountNumber = random.nextInt(maxNumber - minNumber) + minNumber;
68-
String state = randomEnum(State.class).toString();
69-
String carType = randomEnum(CarType.class).toString();
70-
71-
JsonObject data = Json.createObjectBuilder()
72-
.add("accountnumber", accountNumber) // This could be looked up in the DB from the tagId?
73-
.add("license-plate", state + "-" + Integer.toString(licNumber)) // This could be looked up in the DB from the tagId?
74-
.add("cartype", carType) // This could be looked up in the DB from the tagId?
75-
.add("tagid", tagId)
76-
.add("timestamp", dateTimeString)
77-
.build();
78-
79-
log.info("Toll Data :" + data.toString());
80-
sendMessage(data);
81-
Thread.sleep(sleepTime);
82-
}
83-
}
9+
public class TollreaderApplication {
8410

8511
public static void main(String[] args) {
8612
SpringApplication.run(TollreaderApplication.class, args);

0 commit comments

Comments
 (0)