Skip to content

Commit 0cd6d59

Browse files
committed
commit
1 parent 987f9a0 commit 0cd6d59

File tree

20 files changed

+644
-5
lines changed

20 files changed

+644
-5
lines changed

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@
6060
<artifactId>spring-security-taglibs</artifactId>
6161
<version>${springsecurity.version}</version>
6262
</dependency>
63-
63+
<dependency>
64+
<groupId>ch.rasc</groupId>
65+
<artifactId>sse-eventbus</artifactId>
66+
<version>1.1.3</version>
67+
</dependency>
6468

6569
<!-- Hibernate -->
6670
<dependency>

src/main/java/com/faizakram/springmvc/configuration/AppConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.context.annotation.Configuration;
88
import org.springframework.context.support.ResourceBundleMessageSource;
99
import org.springframework.format.FormatterRegistry;
10+
import org.springframework.scheduling.annotation.EnableScheduling;
1011
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1112
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
1213
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@@ -17,7 +18,10 @@
1718

1819
import com.faizakram.springmvc.converter.RoleToUserProfileConverter;
1920

21+
import ch.rasc.sse.eventbus.config.EnableSseEventBus;
2022

23+
@EnableScheduling
24+
@EnableSseEventBus
2125
@Configuration
2226
@EnableWebMvc
2327
@ComponentScan(basePackages = "com.faizakram.springmvc")

src/main/java/com/faizakram/springmvc/configuration/HibernateConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
@Configuration
2020
@EnableTransactionManagement
21-
@ComponentScan({ "com.faizakram.springmvc.configuration" })
21+
@ComponentScan({ "com.faizakram.springmvc" })
2222
@PropertySource(value = { "classpath:application.properties" })
2323
public class HibernateConfiguration {
2424

src/main/java/com/faizakram/springmvc/controller/AppController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public String listUsers(ModelMap model) {
6363
model.addAttribute("loggedinuser", getPrincipal());
6464
return "userslist";
6565
}
66+
67+
@RequestMapping(value = "/notification", method = RequestMethod.GET)
68+
public String notification(ModelMap model) {
69+
return "notification";
70+
}
6671

6772
/**
6873
* This method will provide the medium to add a new user.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.faizakram.springmvc.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestHeader;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
10+
11+
import com.faizakram.springmvc.service.DataEmitterService;
12+
13+
import ch.rasc.sse.eventbus.SseEvent;
14+
import ch.rasc.sse.eventbus.SseEventBus;
15+
16+
@RestController
17+
public class SseController {
18+
@Autowired
19+
private SseEventBus eventBus;
20+
21+
@Autowired
22+
private DataEmitterService dataEmitterService;
23+
24+
/*public SseController(SseEventBus eventBus) {
25+
this.eventBus = eventBus;
26+
}*/
27+
28+
@RequestMapping(value = "/register/{id}",method = RequestMethod.GET)
29+
public SseEmitter register(@PathVariable("id") String id, @RequestHeader(value = "User-Agent") String userAgent) {
30+
dataEmitterService.addListInfo(id);
31+
System.out.println("TEST");
32+
if (userAgent.contains("Edge/")) {
33+
return this.eventBus.createSseEmitter(id, 180_000L, false, true, SseEvent.DEFAULT_EVENT);
34+
}
35+
return this.eventBus.createSseEmitter(id, SseEvent.DEFAULT_EVENT);
36+
}
37+
38+
39+
40+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.faizakram.springmvc.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.ApplicationEventPublisher;
9+
import org.springframework.scheduling.annotation.Scheduled;
10+
import org.springframework.stereotype.Service;
11+
12+
import ch.rasc.sse.eventbus.SseEvent;
13+
import ch.rasc.sse.eventbus.SseEvent.Builder;
14+
15+
@Service
16+
public class DataEmitterService {
17+
18+
@Autowired
19+
private ApplicationEventPublisher eventPublisher;
20+
private List<String> list = new ArrayList<String>();
21+
private final static Random random = new Random();
22+
23+
public void addListInfo(String id) {
24+
list.add(id);
25+
}
26+
27+
private String randomData() {
28+
StringBuilder sb = new StringBuilder("[");
29+
for (int i = 0; i < 5; i++) {
30+
sb.append(random.nextInt(31));
31+
sb.append(",");
32+
}
33+
sb.replace(sb.length() - 1, sb.length(), "]");
34+
return sb.toString();
35+
}
36+
37+
@Scheduled(initialDelay = 2000, fixedRate = 5_000)
38+
public void sendData() {
39+
40+
for (String str : list) {
41+
if (list.indexOf(str) == 0) // this will help different Client
42+
43+
// this.eventPublisher.publishEvent(SseEvent.builder().addClientId(str).event("DataOnline").data("Goa").build());//
44+
// You Call also pass with event name
45+
46+
this.eventPublisher.publishEvent(SseEvent.builder().addClientId(str).data(randomData()).build()); // Default
47+
// Event
48+
// name
49+
// (message)
50+
else
51+
// this.eventPublisher.publishEvent(SseEvent.builder().addClientId(str).event("DataOnline").data("Goa").build());//
52+
// You Call also pass with event name
53+
this.eventPublisher.publishEvent(SseEvent.builder().addClientId(str).data(randomData()).build()); // Default
54+
// Event
55+
// name
56+
// (message)
57+
}
58+
59+
}
60+
61+
}

src/main/java/com/faizakram/springmvc/service/UserProfileServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.faizakram.springmvc.model.UserProfile;
1111

1212

13-
@Service("userProfileService")
13+
@Service
1414
@Transactional
1515
public class UserProfileServiceImpl implements UserProfileService{
1616

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>sse-eventbus-demo with echarts</title>
5+
6+
<style>
7+
body div {
8+
padding: 5px;
9+
display: inline-block;
10+
height: 150px;
11+
width: 230px;
12+
border: 1px solid lightgrey;
13+
}
14+
</style>
15+
</head>
16+
17+
<body style="padding: 40px">
18+
<div id="chart1"></div>
19+
<div id="chart2"></div>
20+
<div id="chart3"></div>
21+
<div id="chart4"></div>
22+
<div id="chart5"></div>
23+
24+
<script src="https://static.rasc.ch/echarts.min.js"></script>
25+
<script src="static/js/eventsource.js"></script>
26+
<script src="static/js/uuid.js"></script>
27+
<script src="static/js/app.js"></script>
28+
</body>
29+
30+
</html>

src/main/webapp/static/js/app.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
function getChartOption(name, threshold) {
2+
return {
3+
series: [ {
4+
startAngle: 180,
5+
endAngle: 0,
6+
center: [ '50%', '90%' ],
7+
radius: 100,
8+
min: 0,
9+
max: 30,
10+
name: 'Serie',
11+
type: 'gauge',
12+
splitNumber: 3,
13+
data: [ {
14+
value: 16,
15+
name: name
16+
} ],
17+
title: {
18+
show: true,
19+
offsetCenter: [ '-100%', '-90%' ],
20+
textStyle: {
21+
color: '#333',
22+
fontSize: 15
23+
}
24+
},
25+
axisLine: {
26+
lineStyle: {
27+
color: [ [ threshold, '#ff4500' ], [ 1, 'lightgreen' ] ],
28+
width: 8
29+
}
30+
},
31+
axisTick: {
32+
length: 11,
33+
lineStyle: {
34+
color: 'auto'
35+
}
36+
},
37+
splitLine: {
38+
length: 15,
39+
lineStyle: {
40+
color: 'auto'
41+
}
42+
},
43+
detail: {
44+
show: true,
45+
offsetCenter: [ '100%', '-100%' ],
46+
textStyle: {
47+
color: 'auto',
48+
fontSize: 25
49+
}
50+
}
51+
52+
} ]
53+
54+
};
55+
}
56+
57+
var names = ['s1', 's2', 's3', 's4', 's5'];
58+
var threshold = [0.1, 0.2, 0.7, 0.5, 0.9];
59+
var gauges = [];
60+
61+
for (let i = 0; i < names.length; i++) {
62+
var chart = echarts.init(document.getElementById('chart'+(i+1)));
63+
chart.setOption(getChartOption(names[i], threshold[i]));
64+
gauges.push(chart);
65+
}
66+
67+
var uuid = uuid.v4();
68+
69+
var eventSource;
70+
71+
window.onbeforeunload = () => {
72+
if (eventSource) {
73+
eventSource.close();
74+
}
75+
}
76+
77+
eventSource = new EventSource(`/SpringMVCHibernateWithSpringSecurity/register/${uuid}`);
78+
eventSource.addEventListener('message', response => {
79+
for (let line of response.data.split('\n')) {
80+
console.log(line);
81+
handleResponse(JSON.parse(line));
82+
}
83+
}, false);
84+
85+
function handleResponse(data) {
86+
for (let i = 0; i < 5; i++) {
87+
gauges[i].setOption({
88+
series: {
89+
data: [ {
90+
name: names[i],
91+
value: data[i]
92+
} ]
93+
}
94+
});
95+
}
96+
}

0 commit comments

Comments
 (0)