Skip to content

Commit 0051d3f

Browse files
committed
polling-publisher service implementation
1 parent c4e9276 commit 0051d3f

File tree

11 files changed

+317
-28
lines changed

11 files changed

+317
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@startuml
2+
@enduml

Diff for: polling-publisher/polling-service/pom.xml

+4-6
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,18 @@
3333
<version>1.26.0-SNAPSHOT</version>
3434
</parent>
3535

36-
<groupId>com.iluwatar</groupId>
3736
<artifactId>polling-service</artifactId>
3837
<version>1.26.0-SNAPSHOT</version>
3938
<packaging>pom</packaging>
4039

4140
<dependencyManagement>
4241
<dependencies>
43-
4442
<dependency>
45-
<groupId>org.mockito</groupId>
46-
<artifactId>mockito-core</artifactId>
47-
<scope>test</scope>
43+
<groupId>com.h2database</groupId>
44+
<artifactId>h2</artifactId>
45+
<scope>runtime</scope>
4846
</dependency>
49-
</dependencies>
47+
</dependencies>
5048
</dependencyManagement>
5149

5250
<build>

Diff for: polling-publisher/polling-service/src/main/java/com/iluwatar/polling-service/App.java renamed to polling-publisher/polling-service/src/main/java/com/iluwatar/polling/App.java

+12-22
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,26 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
package com.iluwatar.polling.publisher;
25+
26+
package com.iluwatar.polling;
27+
28+
import org.springframework.boot.SpringApplication;
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.scheduling.annotation.EnableScheduling;
2631

2732
/**
2833
* Polling-Publisher pattern paradigm.
2934
*/
30-
public class App {
31-
32-
private static final String DEFAULT_URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/polling-publisher/README.md";
33-
34-
35-
App() {
36-
37-
}
38-
39-
int sum(int x, int y){
40-
return x+y;
41-
}
35+
@SpringBootApplication
36+
@EnableScheduling
37+
public class App{
4238

4339
/**
4440
* Program entry point.
4541
*
46-
* @param args arguments
47-
* @throws InterruptedException if main thread is interrupted.
42+
* @param args command line args.
4843
*/
49-
public static void main(String[] args) throws InterruptedException {
50-
var app = new App();
51-
try {
52-
System.out.println("checking...");
53-
} finally {
54-
System.out.println("finally-block");
55-
}
44+
public static void main(String[] args){
45+
SpringApplication.run(PollingApplication.class, args);
5646
}
5747
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.polling;
27+
28+
import org.springframework.stereotype.Repository;
29+
30+
/**
31+
* Data repository to keep or store data.
32+
* */
33+
@Repository
34+
public class DataRepository{
35+
// Simulate a repository for data access
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.polling;
27+
28+
import java.util.List;
29+
import org.springframework.stereotype.Service;
30+
31+
/**
32+
* This class is responsible for keep the events.
33+
*/
34+
@Service
35+
public class DataSourceService{
36+
37+
/**
38+
* Function to fetch data from DataRepository.
39+
* */
40+
public String fetchData(){
41+
// Simulate fetching data from a data source
42+
return "Sample Data " + System.currentTimeMillis();
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.polling;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.kafka.core.KafkaTemplate;
30+
import org.springframework.stereotype.Service;
31+
32+
33+
/**
34+
* This class is responsible for sending messages to Kafka.
35+
*/
36+
@Service
37+
public class KafkaProducer{
38+
39+
private final KafkaTemplate<String, String> kafkaTemplate;
40+
41+
public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate){
42+
this.kafkaTemplate = kafkaTemplate;
43+
}
44+
45+
/**
46+
* Sends a message to the Kafka topic.
47+
*
48+
* @param message The message to send.
49+
*/
50+
public void sendMessage(String message){
51+
kafkaTemplate.send("updates", message);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.polling;
27+
28+
import org.springframework.web.bind.annotation.GetMapping;
29+
import org.springframework.web.bind.annotation.RestController;
30+
31+
/**
32+
* This class is responsible for contolling polling tasks.
33+
*/
34+
@RestController
35+
public class PollingController{
36+
37+
@GetMapping("/health")
38+
public String healthCheck(){
39+
return "Polling Service is up and running!";
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.polling;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.scheduling.annotation.Scheduled;
30+
import org.springframework.stereotype.Component;
31+
32+
/**
33+
* This class is responsible for scheduling polling tasks.
34+
*/
35+
@Component
36+
public class PollingScheduler{
37+
38+
@Autowired
39+
private DataSourceService dataSourceService;
40+
41+
@Autowired
42+
private KafkaProducer kafkaProducer;
43+
44+
/**
45+
* schedular for poll data on each 5 second.
46+
* */
47+
@Scheduled(fixedRate = 5000) // Poll every 5 seconds
48+
public void pollDataSource(){
49+
String data = dataSourceService.fetchData();
50+
if(data != null){
51+
kafkaProducer.sendMessage(data);
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.polling;
26+
27+
public class AppTest{
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.polling;
27+
28+
import org.junit.jupiter.api.Test;
29+
import static org.junit.jupiter.api.Assertions;
30+
31+
/*
32+
* this class contains the unit test for DataSoourceServiceTest class.
33+
*/
34+
public class DataSourceServiceTest{
35+
36+
DataSourceService dataSourceService = new DataSourceService();
37+
38+
@Test
39+
public void test1(){
40+
System.out.println(".()");
41+
}
42+
}

Diff for: polling-publisher/subscriber-service/src/test/java/com/iluwatar/subscriber/AppTest.java

Whitespace-only changes.

0 commit comments

Comments
 (0)