Skip to content

Commit e9a8659

Browse files
committed
Added JMS
1 parent f192d26 commit e9a8659

File tree

9 files changed

+271
-93
lines changed

9 files changed

+271
-93
lines changed

docker-compose.yml

+96-60
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,67 @@ version: '3.9'
44
services:
55
# App backend service
66
#app:
7-
# This service depends on postgres db and keycloak auth. Start that first.
8-
# depends_on:
9-
# db:
10-
# condition: service_healthy
11-
#keycloak:
12-
# condition: service_started
13-
#image: spring-boot-keycloak-docker-postgres:latest
14-
#build:
15-
# context: ./
16-
# dockerfile: "Dockerfile"
17-
# Give the container the name web-app. You can change to something else.
18-
#container_name: web-app
19-
# Forward the exposed port 8080 on the container to port 8080 on the host machine
20-
#ports:
21-
# - "0.0.0.0:8088:8080/tcp"
22-
# - target: 8080
23-
# host_ip: 0.0.0.0
24-
# published: 8088
25-
#protocol: tcp
26-
#mode: host
27-
#ports:
28-
# - "8080:8080"
29-
#networks:
30-
# - backend
31-
# entrypoint: [ "java", "-Xms512m", "-Xmx1g", "-jar" ]
7+
# This service depends on postgres db and keycloak auth. Start that first.
8+
# depends_on:
9+
# db:
10+
# condition: service_healthy
11+
#keycloak:
12+
# condition: service_started
13+
#image: spring-boot-keycloak-docker-postgres:latest
14+
#build:
15+
# context: ./
16+
# dockerfile: "Dockerfile"
17+
# Give the container the name web-app. You can change to something else.
18+
#container_name: web-app
19+
# Forward the exposed port 8080 on the container to port 8080 on the host machine
20+
#ports:
21+
# - "0.0.0.0:8088:8080/tcp"
22+
# - target: 8080
23+
# host_ip: 0.0.0.0
24+
# published: 8088
25+
#protocol: tcp
26+
#mode: host
27+
#ports:
28+
# - "8080:8080"
29+
#networks:
30+
# - backend
31+
# entrypoint: [ "java", "-Xms512m", "-Xmx1g", "-jar" ]
3232
# Database Service (Postgres)
3333
#db:
34-
# Give the container the name postgres-db. You can change to something else.
35-
# container_name: postgres-db
36-
# Use the Docker Image postgres. This will pull the 14 version.
37-
#image: postgres:14-alpine
38-
#healthcheck:
39-
# test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U" ]
40-
#timeout: 45s
41-
#interval: 10s
42-
#retries: 10
43-
#restart: always
44-
# Set a volume some that database is not lost after shutting down the container.
45-
# I used the name postgres-data, but you can change it to something else.
46-
#volumes:
47-
# - postgres_data_keycloak:/var/lib/postgresql/data
48-
#networks:
49-
# - backend
50-
#network_mode: host
51-
# Maps port 5432 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
52-
#ports:
53-
# - "5432:5432"
54-
# Set up the username, password, and database name. You can change these values.
55-
#environment:
56-
# POSTGRES_USER: postgres
57-
#POSTGRES_PASSWORD: postgres
58-
#POSTGRES_DB: automobiles
59-
#PGDATA: /var/lib/postgresql/data/pgdata
34+
# Give the container the name postgres-db. You can change to something else.
35+
# container_name: postgres-db
36+
# Use the Docker Image postgres. This will pull the 14 version.
37+
#image: postgres:14-alpine
38+
#healthcheck:
39+
# test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U" ]
40+
#timeout: 45s
41+
#interval: 10s
42+
#retries: 10
43+
#restart: always
44+
# Set a volume some that database is not lost after shutting down the container.
45+
# I used the name postgres-data, but you can change it to something else.
46+
#volumes:
47+
# - postgres_data_keycloak:/var/lib/postgresql/data
48+
#networks:
49+
# - backend
50+
#network_mode: host
51+
# Maps port 5432 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
52+
#ports:
53+
# - "5432:5432"
54+
# Set up the username, password, and database name. You can change these values.
55+
#environment:
56+
# POSTGRES_USER: postgres
57+
#POSTGRES_PASSWORD: postgres
58+
#POSTGRES_DB: automobiles
59+
#PGDATA: /var/lib/postgresql/data/pgdata
6060
# Auth service
6161
keycloak:
6262
container_name: keycloak-auth
6363
image: quay.io/keycloak/keycloak:22.0.1
64-
#build:
65-
# context: .
66-
#args:
67-
# KEYCLOAK_VERSION: 22.0.1
64+
#build:
65+
# context: .
66+
#args:
67+
# KEYCLOAK_VERSION: 22.0.1
6868
command:
6969
- "start-dev"
7070
ports:
@@ -81,7 +81,7 @@ services:
8181
KC_DB_PASSWORD: password
8282
KC_HEALTH_ENABLED: true
8383
depends_on:
84-
- keycloak-db
84+
- keycloak-db
8585
#volumes:
8686
# - /home/keycloak/automobile-realm.json:/opt/keycloak/data/import/automobile-realm.json
8787
# Database Service (Postgres) for Keycloak
@@ -96,22 +96,58 @@ services:
9696
POSTGRES_DB: keycloak
9797
POSTGRES_USER: keycloak
9898
POSTGRES_PASSWORD: password
99-
networks:
100-
- keycloak
99+
networks: [ keycloak ]
101100
healthcheck:
102101
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U" ]
103102
timeout: 45s
104103
interval: 10s
105104
retries: 10
106105

106+
activemq:
107+
image: webcenter/activemq:latest
108+
ports:
109+
# mqtt
110+
- "1883:1883"
111+
# amqp
112+
- "5672:5672"
113+
# ui
114+
- "8161:8161"
115+
# stomp
116+
- "61613:61613"
117+
# ws
118+
- "61614:61614"
119+
# jms
120+
- "61616:61616"
121+
networks: [ activemq ]
122+
volumes: [ "activemq-data:/opt/activemq/conf", "activemq-data:/data/activemq", "activemq-data:/var/log/activemq" ]
123+
environment:
124+
ACTIVEMQ_REMOVE_DEFAULT_ACCOUNT: "true"
125+
ACTIVEMQ_ADMIN_LOGIN: admin
126+
ACTIVEMQ_ADMIN_PASSWORD: password
127+
ACTIVEMQ_WRITE_LOGIN: write
128+
ACTIVEMQ_WRITE_PASSWORD: password
129+
ACTIVEMQ_READ_LOGIN: read
130+
ACTIVEMQ_READ_PASSWORD: password
131+
ACTIVEMQ_JMX_LOGIN: jmx
132+
ACTIVEMQ_JMX_PASSWORD: password
133+
134+
ACTIVEMQ_STATIC_TOPICS: static-topic-1;static-topic-2;autoTopic
135+
ACTIVEMQ_STATIC_QUEUES: static-queue-1;static-queue-2
136+
ACTIVEMQ_ENABLED_SCHEDULER: "true"
137+
ACTIVEMQ_MIN_MEMORY: 512
138+
ACTIVEMQ_MAX_MEMORY: 2048
139+
107140
networks:
108-
# backend:
141+
# backend:
109142
# name: app
110-
# driver: bridge
143+
# driver: bridge
111144
keycloak:
112145
name: keycloak
113146
driver: bridge
147+
activemq: { }
114148

115149
volumes:
116150
postgres_data_keycloak:
117151
driver: local
152+
activemq-data:
153+
driver: local

pom.xml

+44-24
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@
8282
<version>${org.project-lombok.version}</version>
8383
<scope>provided</scope>
8484
</dependency>
85+
<!-- activemq -->
86+
<dependency>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-starter-activemq</artifactId>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.apache.activemq</groupId>
92+
<artifactId>activemq-broker</artifactId>
93+
</dependency>
94+
<dependency>
95+
<groupId>com.fasterxml.jackson.core</groupId>
96+
<artifactId>jackson-databind</artifactId>
97+
</dependency>
98+
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
99+
<dependency>
100+
<groupId>com.fasterxml.jackson.datatype</groupId>
101+
<artifactId>jackson-datatype-jsr310</artifactId>
102+
<version>2.15.2</version>
103+
</dependency>
104+
85105
</dependencies>
86106

87107
<build>
@@ -113,30 +133,30 @@
113133
</compilerArgs>
114134
</configuration>
115135
</plugin>
116-
<!-- <plugin>
117-
<groupId>io.fabric8</groupId>
118-
<artifactId>docker-maven-plugin</artifactId>
119-
<version>0.43.0</version>
120-
<executions>
121-
<execution>
122-
<id>docker-build</id>
123-
<phase>package</phase>
124-
<goals>
125-
<goal>build</goal>
126-
</goals>
127-
<configuration>
128-
<images>
129-
<image>
130-
<name>spring-boot-keycloak-docker-postgres</name>
131-
<build>
132-
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
133-
</build>
134-
</image>
135-
</images>
136-
</configuration>
137-
</execution>
138-
</executions>
139-
</plugin>-->
136+
<!-- <plugin>
137+
<groupId>io.fabric8</groupId>
138+
<artifactId>docker-maven-plugin</artifactId>
139+
<version>0.43.0</version>
140+
<executions>
141+
<execution>
142+
<id>docker-build</id>
143+
<phase>package</phase>
144+
<goals>
145+
<goal>build</goal>
146+
</goals>
147+
<configuration>
148+
<images>
149+
<image>
150+
<name>spring-boot-keycloak-docker-postgres</name>
151+
<build>
152+
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
153+
</build>
154+
</image>
155+
</images>
156+
</configuration>
157+
</execution>
158+
</executions>
159+
</plugin>-->
140160

141161
</plugins>
142162
</build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.kaluzny.demo.config;
2+
3+
import org.apache.activemq.ActiveMQConnectionFactory;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.jms.annotation.EnableJms;
8+
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
9+
import org.springframework.jms.connection.CachingConnectionFactory;
10+
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
11+
import org.springframework.jms.support.converter.MessageConverter;
12+
import org.springframework.jms.support.converter.MessageType;
13+
14+
@Configuration
15+
@EnableJms
16+
public class JMSConfig {
17+
18+
@Value("${spring.activemq.broker-url}")
19+
private String brokerUrl;
20+
21+
@Bean
22+
public DefaultJmsListenerContainerFactory automobileJmsContFactory() {
23+
DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
24+
containerFactory.setPubSubDomain(true);
25+
containerFactory.setConnectionFactory(connectionFactory());
26+
containerFactory.setMessageConverter(jacksonJmsMsgConverter());
27+
containerFactory.setSubscriptionDurable(true);
28+
return containerFactory;
29+
}
30+
31+
@Bean
32+
public CachingConnectionFactory connectionFactory() {
33+
ActiveMQConnectionFactory activeMQConnFactory = new ActiveMQConnectionFactory();
34+
activeMQConnFactory.setBrokerURL(brokerUrl);
35+
CachingConnectionFactory factory = new CachingConnectionFactory();
36+
factory.setTargetConnectionFactory(activeMQConnFactory);
37+
factory.setClientId("client123");
38+
return factory;
39+
}
40+
41+
@Bean
42+
public MessageConverter jacksonJmsMsgConverter() {
43+
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
44+
converter.setTargetType(MessageType.TEXT);
45+
converter.setTypeIdPropertyName("_type");
46+
return converter;
47+
}
48+
}

src/main/java/com/kaluzny/demo/config/SecurityConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
3434
.jwtAuthenticationConverter(jwt -> {
3535
Map<String, Collection<String>> realmAccess = jwt.getClaim("realm_access");
3636
Collection<String> roles = realmAccess.get("roles");
37+
3738
var grantedAuthorities = roles.stream()
3839
.map(role -> new SimpleGrantedAuthority("ROLE_" + role))
3940
.collect(Collectors.toList());
41+
4042
return new JwtAuthenticationToken(jwt, grantedAuthorities);
4143
})));
4244
return httpSecurity.build();

src/main/java/com/kaluzny/demo/domain/Automobile.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package com.kaluzny.demo.domain;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
6+
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
7+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
38
import io.swagger.v3.oas.annotations.media.Schema;
49
import jakarta.persistence.*;
510
import jakarta.validation.constraints.Size;
6-
import lombok.AllArgsConstructor;
7-
import lombok.Getter;
8-
import lombok.NoArgsConstructor;
9-
import lombok.Setter;
11+
import lombok.*;
1012

11-
import java.time.Instant;
13+
import java.time.LocalDateTime;
1214

1315

1416
@Entity
1517
@Getter
1618
@Setter
19+
@ToString
1720
@AllArgsConstructor
1821
@NoArgsConstructor
22+
@Builder
1923
@Schema(name = "Automobile", description = "Data object for an automobile", oneOf = Automobile.class)
2024
public class Automobile {
2125

@@ -32,9 +36,15 @@ public class Automobile {
3236
@Size(max = 50)
3337
private String color;
3438

35-
private Instant creationDate = Instant.now();
39+
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
40+
@JsonSerialize(using = LocalDateTimeSerializer.class)
41+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
42+
private LocalDateTime creationDate = LocalDateTime.now();
3643

37-
private Instant updateDate = Instant.now();
44+
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
45+
@JsonSerialize(using = LocalDateTimeSerializer.class)
46+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
47+
private LocalDateTime updateDate = LocalDateTime.now();
3848

3949
@Column(name = "original_color")
4050
private Boolean originalColor = Boolean.TRUE;

0 commit comments

Comments
 (0)