Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
FMS_Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bashar.hussein
FMS_Project
Commits
ae53ceb5
Commit
ae53ceb5
authored
May 11, 2023
by
Bashar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Rethink Realtime Ability
parent
517f28ba
Changes
13
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
369 additions
and
29 deletions
+369
-29
ConsumerApplication.java
...c/main/java/com/example/consumer/ConsumerApplication.java
+31
-1
SocketTextHandler.java
...com/example/consumer/configuration/SocketTextHandler.java
+42
-11
WebSocketConfig.java
...a/com/example/consumer/configuration/WebSocketConfig.java
+9
-10
RethinkController.java
...va/com/example/consumer/controller/RethinkController.java
+3
-2
RethinkDBConnectionFactory.java
.../example/consumer/factory/RethinkDBConnectionFactory.java
+14
-0
RethinkChange.java
...n/java/com/example/consumer/repository/RethinkChange.java
+17
-0
DbInitializer.java
...ain/java/com/example/consumer/services/DbInitializer.java
+3
-1
RethinkDBService.java
.../java/com/example/consumer/services/RethinkDBService.java
+13
-0
application.properties
consumer/src/main/resources/application.properties
+2
-2
9ab94c3e-b3cf-49d9-a5c1-98af7df44d3e
data/rethinkdb_data/9ab94c3e-b3cf-49d9-a5c1-98af7df44d3e
+0
-0
log_file
data/rethinkdb_data/log_file
+233
-0
metadata
data/rethinkdb_data/metadata
+0
-0
docker-compose-elk.yaml
docker-compose-elk.yaml
+2
-2
No files found.
consumer/src/main/java/com/example/consumer/ConsumerApplication.java
View file @
ae53ceb5
package
com
.
example
.
consumer
;
import
com.example.consumer.configuration.SocketTextHandler
;
import
com.example.consumer.factory.RethinkDBConnectionFactory
;
import
com.example.consumer.repository.RethinkChange
;
import
com.rethinkdb.RethinkDB
;
import
com.rethinkdb.net.Connection
;
import
com.rethinkdb.net.Cursor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.kafka.annotation.KafkaListener
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
@SpringBootApplication
public
class
ConsumerApplication
{
public
class
ConsumerApplication
implements
CommandLineRunner
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
ConsumerApplication
.
class
,
args
);
}
private
final
RethinkDB
r
=
RethinkDB
.
r
;
@Autowired
public
RethinkDBConnectionFactory
connectionFactory
;
@Autowired
private
SocketTextHandler
socket_server
;
@Override
public
void
run
(
String
...
args
)
throws
Exception
{
Connection
connection
=
connectionFactory
.
getConnection
();
Cursor
<
RethinkChange
>
changeCursor
=
r
.
db
(
"my_database"
).
table
(
"my_table"
).
changes
().
optArg
(
"include_initial"
,
true
).
run
(
connection
,
RethinkChange
.
class
);
List
<
Map
<
String
,
Object
>>
result
=
new
ArrayList
<>();
for
(
RethinkChange
change
:
changeCursor
){
System
.
out
.
println
(
"Something Changed"
);
result
.
add
(
change
.
getNew_val
());
socket_server
.
broadcast
(
result
);
}
}
// @KafkaListener(topics = "Test")
// public void handleNotification(String s) {
//
...
...
consumer/src/main/java/com/example/consumer/configuration/SocketTextHandler.java
View file @
ae53ceb5
package
com
.
example
.
consumer
.
configuration
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.CopyOnWriteArrayList
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectWriter
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.socket.CloseStatus
;
import
org.springframework.web.socket.TextMessage
;
import
org.springframework.web.socket.WebSocketMessage
;
import
org.springframework.web.socket.WebSocketSession
;
import
org.springframework.web.socket.handler.TextWebSocketHandler
;
@Component
public
class
SocketTextHandler
{
// extends TextWebSocketHandler {
//
// @Override
// public void handleTextMessage(WebSocketSession session, TextMessage message)
// throws InterruptedException, IOException {
//
// String payload = message.getPayload();
// JSONObject jsonObject = new JSONObject(payload);
// session.sendMessage(new TextMessage("Hi " + jsonObject.get("user") + " how may we help you?"));
// }
public
class
SocketTextHandler
extends
TextWebSocketHandler
{
private
List
<
WebSocketSession
>
sessions
=
new
CopyOnWriteArrayList
<>();
@Override
public
void
afterConnectionEstablished
(
WebSocketSession
session
)
throws
Exception
{
System
.
out
.
println
(
"New connection from "
+
session
.
getRemoteAddress
());
sessions
.
add
(
session
);
}
@Override
protected
void
handleTextMessage
(
WebSocketSession
session
,
TextMessage
message
)
throws
Exception
{
System
.
out
.
println
(
"Received message from "
+
session
.
getRemoteAddress
()
+
": "
+
message
.
getPayload
());
// Handle incoming message
}
@Override
public
void
afterConnectionClosed
(
WebSocketSession
session
,
CloseStatus
status
)
throws
Exception
{
System
.
out
.
println
(
"Connection closed to "
+
session
.
getRemoteAddress
()
+
" with status "
+
status
);
sessions
.
remove
(
session
);
}
public
void
broadcast
(
List
<
Map
<
String
,
Object
>>
message
)
throws
IOException
{
System
.
out
.
println
(
"Hey Broadcasting"
);
System
.
out
.
println
(
message
);
ObjectWriter
ow
=
new
ObjectMapper
().
writer
().
withDefaultPrettyPrinter
();
String
json
=
ow
.
writeValueAsString
(
message
);
System
.
out
.
println
(
json
);
TextMessage
s
=
new
TextMessage
(
json
);
System
.
out
.
println
(
s
);
for
(
WebSocketSession
session
:
sessions
)
{
if
(
session
.
isOpen
())
{
session
.
sendMessage
(
s
);
}
}
}
}
consumer/src/main/java/com/example/consumer/configuration/WebSocketConfig.java
View file @
ae53ceb5
package
com
.
example
.
consumer
.
configuration
;
import
com.example.consumer.services.RethinkDBService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.messaging.simp.config.MessageBrokerRegistry
;
import
org.springframework.web.socket.config.annotation.*
;
@Configuration
public
class
WebSocketConfig
implements
WebSocketMessageBrokerConfigurer
{
@EnableWebSocket
public
class
WebSocketConfig
implements
WebSocketConfigurer
{
@Autowired
private
SocketTextHandler
socket_server
;
@Override
public
void
registerStompEndpoints
(
StompEndpointRegistry
registry
)
{
registry
.
addEndpoint
(
"/ws"
).
setAllowedOriginPatterns
(
"*"
).
withSockJS
();
}
@Override
public
void
configureMessageBroker
(
MessageBrokerRegistry
registry
)
{
registry
.
setApplicationDestinationPrefixes
(
"/app"
);
registry
.
enableSimpleBroker
(
"/traps"
,
"/changes"
);
registry
.
setUserDestinationPrefix
(
"/changes"
);
public
void
registerWebSocketHandlers
(
WebSocketHandlerRegistry
registry
)
{
System
.
out
.
println
(
"HEYYYYYYYYYYYYYYYYYY"
);
registry
.
addHandler
(
socket_server
,
"/my-websocket"
).
setAllowedOrigins
(
"*"
);
}
}
consumer/src/main/java/com/example/consumer/controller/RethinkController.java
View file @
ae53ceb5
...
...
@@ -15,10 +15,10 @@ import java.util.Map;
@RequestMapping
(
"/api/rethink"
)
public
class
RethinkController
{
@Autowired
private
RethinkDBService
rethinkDBService
;
private
RethinkDBService
rethinkDBService
;
@PostMapping
(
"/data"
)
@PostMapping
(
"/data"
)
public
ResponseEntity
<
String
>
saveData
(
@RequestBody
Map
<
String
,
Object
>
data
)
{
rethinkDBService
.
saveData
(
"my_database"
,
"my_table"
,
data
);
return
ResponseEntity
.
ok
(
"Data saved successfully in RethinkDB"
);
...
...
@@ -27,6 +27,7 @@ public class RethinkController {
@GetMapping
(
"/data"
)
public
ResponseEntity
<
List
<
Map
<
String
,
Object
>>>
getData
()
{
List
<
Map
<
String
,
Object
>>
result
=
rethinkDBService
.
getData
(
"my_database"
,
"my_table"
);
System
.
out
.
println
(
result
);
if
(
result
!=
null
)
{
return
ResponseEntity
.
ok
(
result
);
}
else
{
...
...
consumer/src/main/java/com/example/consumer/factory/RethinkDBConnectionFactory.java
View file @
ae53ceb5
...
...
@@ -2,12 +2,15 @@ package com.example.consumer.factory;
import
com.rethinkdb.RethinkDB
;
import
com.rethinkdb.net.Connection
;
import
com.rethinkdb.net.Cursor
;
import
jakarta.annotation.PostConstruct
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.TimeoutException
;
@Component
...
...
@@ -28,6 +31,17 @@ public class RethinkDBConnectionFactory {
try
{
connection
=
r
.
connection
().
hostname
(
host
).
port
(
port
).
connect
();
log
.
info
(
"RethinkDB connected successfully"
);
List
<
String
>
dbList
=
r
.
dbList
().
run
(
connection
);
if
(!
dbList
.
contains
(
"my_database"
))
{
System
.
out
.
println
(
"Creating DATABASE Heeeeeeeeeeeeeeeeeeeeeeeeeere"
);
r
.
dbCreate
(
"my_database"
).
run
(
connection
);
}
List
<
String
>
tables
=
r
.
db
(
"my_database"
).
tableList
().
run
(
connection
);
if
(!
tables
.
contains
(
"my_table"
))
{
System
.
out
.
println
(
"Creating Table Heeeeeeeeeeeeeeeeeeeeeeeeeere"
);
r
.
db
(
"my_database"
).
tableCreate
(
"my_table"
).
run
(
connection
);
//r.db("my_database").table("my_table").indexCreate("trap").run(connection);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"Error connecting to RethinkDB"
,
e
);
}
...
...
consumer/src/main/java/com/example/consumer/repository/RethinkChange.java
0 → 100644
View file @
ae53ceb5
package
com
.
example
.
consumer
.
repository
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
java.util.Map
;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public
class
RethinkChange
{
private
Map
<
String
,
Object
>
new_val
;
private
Map
<
String
,
Object
>
old_val
;
}
consumer/src/main/java/com/example/consumer/services/DbInitializer.java
View file @
ae53ceb5
...
...
@@ -20,17 +20,19 @@ public class DbInitializer implements InitializingBean {
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
createDb
();
}
private
void
createDb
()
{
System
.
out
.
println
(
"We are initializing heeeeeeeeeeeeeeeeeeeeeer"
);
Connection
connection
=
connectionFactory
.
getConnection
();
List
<
String
>
dbList
=
r
.
dbList
().
run
(
connection
);
if
(!
dbList
.
contains
(
"new_database"
))
{
System
.
out
.
println
(
"Creating DATABASE Heeeeeeeeeeeeeeeeeeeeeeeeeere"
);
r
.
dbCreate
(
"new_database"
).
run
(
connection
);
}
List
<
String
>
tables
=
r
.
db
(
"new_database"
).
tableList
().
run
(
connection
);
if
(!
tables
.
contains
(
"new_table"
))
{
System
.
out
.
println
(
"Creating Table Heeeeeeeeeeeeeeeeeeeeeeeeeere"
);
r
.
db
(
"new_database"
).
tableCreate
(
"new_table"
).
run
(
connection
);
r
.
db
(
"new_database"
).
table
(
"new_table"
).
indexCreate
(
"trap"
).
run
(
connection
);
}
...
...
consumer/src/main/java/com/example/consumer/services/RethinkDBService.java
View file @
ae53ceb5
package
com
.
example
.
consumer
.
services
;
import
com.example.consumer.configuration.SocketTextHandler
;
import
com.example.consumer.factory.RethinkDBConnectionFactory
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.rethinkdb.RethinkDB
;
import
com.rethinkdb.gen.ast.Json
;
import
com.rethinkdb.gen.ast.Table
;
import
com.rethinkdb.net.Cursor
;
import
org.reactivestreams.Subscription
;
import
org.slf4j.Logger
;
...
...
@@ -42,6 +44,7 @@ public class RethinkDBService {
String
jsonString
=
"{\"trap\":\" "
+
message
+
"\"}"
;
JsonNode
jsonNode
=
objectMapper
.
readTree
(
jsonString
);
// Create a RethinkDB document object using the parsed JSON object
Map
<
String
,
Object
>
document
=
objectMapper
.
convertValue
(
jsonNode
,
Map
.
class
);
r
.
db
(
"my_database"
).
table
(
"my_table"
).
insert
(
document
).
run
(
connectionFactory
.
getConnection
());
...
...
@@ -71,7 +74,17 @@ public class RethinkDBService {
return
null
;
}
}
//Connection conn = connectionFactory.getConnection();
//Cursor<Map<String, Object> > changeCursor = r.db(database).table(table).changes().run(conn);
/*for (Object change : changeCursor) {
result.add((Map<String, Object>) change);
}
try {
socket_server.broadcast(result);
} catch (IOException e) {
throw new RuntimeException(e);
}*/
}
consumer/src/main/resources/application.properties
View file @
ae53ceb5
server.port
=
2005
server.port
=
4164
spring.kafka.bootstrap-servers
=
localhost:9092
spring.kafka.template.default-topic
=
notificationTopic
...
...
@@ -10,4 +10,4 @@ rethinkdb.host = localhost
rethinkdb.port
=
28015
spring.data.elasticsearch.cluster-names
=
trapsService
spring.data.elasticsearch.cluster-node
=
localhost:9200
\ No newline at end of file
spring.data.elasticsearch.cluster-node
=
localhost:9200
data/rethinkdb_data/
0d2cdb10-8d12-4307-bcba-7fb2b6e6d142
→
data/rethinkdb_data/
9ab94c3e-b3cf-49d9-a5c1-98af7df44d3e
View file @
ae53ceb5
No preview for this file type
data/rethinkdb_data/log_file
View file @
ae53ceb5
This diff is collapsed.
Click to expand it.
data/rethinkdb_data/metadata
View file @
ae53ceb5
No preview for this file type
docker-compose-elk.yaml
View file @
ae53ceb5
...
...
@@ -3,7 +3,7 @@ version: "3"
services
:
elasticsearch
:
image
:
docker.elastic.co/elasticsearch/elasticsearch:8.3.3
image
:
elasticsearch:8.7.1
container_name
:
elasticsearch
environment
:
-
bootstrap.memory_lock=true
...
...
@@ -18,7 +18,7 @@ services:
-
elastic
kibana
:
image
:
docker.elastic.co/kibana/kibana:8.3.3
image
:
kibana:8.7.1
container_name
:
kibana
ports
:
-
"
5601:5601"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment