-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from Jolvre/feat/sse
Feat/sse
- Loading branch information
Showing
20 changed files
with
578 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/example/jolvre/common/config/KafkaConsumerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example.jolvre.common.config; | ||
|
||
import com.example.jolvre.notification.entity.NotificationMessage; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
import org.springframework.kafka.support.serializer.JsonDeserializer; | ||
|
||
@Configuration | ||
public class KafkaConsumerConfig { | ||
@Value("${spring.kafka.bootstrap-servers}") | ||
private String bootstrapServers; | ||
|
||
@Value("${spring.kafka.consumer.group-id}") | ||
private String groupId; | ||
|
||
@Bean | ||
public ConsumerFactory<String, NotificationMessage> consumerFactory() { | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
config.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); | ||
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
// config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest"); | ||
JsonDeserializer<NotificationMessage> deserializer = new JsonDeserializer<>(NotificationMessage.class); | ||
deserializer.addTrustedPackages("com.example.jolvre"); | ||
return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(), deserializer); | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, NotificationMessage> kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, NotificationMessage> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setConsumerFactory(consumerFactory()); | ||
return factory; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/jolvre/common/config/KafkaProducerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.jolvre.common.config; | ||
|
||
import com.example.jolvre.notification.entity.NotificationMessage; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.core.ProducerFactory; | ||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
@Configuration | ||
public class KafkaProducerConfig { | ||
@Value("${spring.kafka.bootstrap-servers}") | ||
private String bootstrapServers; | ||
|
||
@Bean | ||
public ProducerFactory<String, NotificationMessage> producerFactory() { | ||
Map<String,Object> configs = new HashMap<>(); | ||
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); | ||
return new DefaultKafkaProducerFactory<>(configs); | ||
} | ||
|
||
@Bean | ||
public KafkaTemplate<String, NotificationMessage> kafkaTemplate() { | ||
return new KafkaTemplate<>(producerFactory()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/example/jolvre/notification/api/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.example.jolvre.notification.api; | ||
|
||
import com.example.jolvre.auth.PrincipalDetails; | ||
import com.example.jolvre.notification.dto.NotificationDTO.NotificationInfoResponses; | ||
import com.example.jolvre.notification.entity.NotificationType; | ||
import com.example.jolvre.notification.service.EmitterService; | ||
import com.example.jolvre.notification.service.NotificationService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
@Tag(name = "알림", description = "알림 설정 및 정보를 관리합니다") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@RequestMapping("/api/v1/notification") | ||
public class NotificationController { | ||
private final NotificationService notificationService; | ||
private final EmitterService emitterService; | ||
|
||
@GetMapping("/test") | ||
public ResponseEntity<Void> test() { | ||
notificationService.commentNotificationCreate(1L, 2L, "야 !", NotificationType.EXHIBIT_COMMENT); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@Operation(summary = "sse 연결", description = "서버와 sse 커넥션을 설정합니다") | ||
@GetMapping(produces = "text/event-stream") | ||
public ResponseEntity<SseEmitter> stream(@AuthenticationPrincipal PrincipalDetails principalDetails | ||
, @RequestHeader(value = "Last-Event-ID", required = false, defaultValue = "") String lastEventId) { | ||
log.info("emitter connect"); | ||
|
||
SseEmitter response = emitterService.addEmitter(String.valueOf(principalDetails.getId()), lastEventId); | ||
|
||
return ResponseEntity.ok(response); | ||
} | ||
|
||
@Operation(summary = "알림 조회", description = "자신의 알림을 조회합니닥") | ||
@GetMapping("/me") | ||
public ResponseEntity<NotificationInfoResponses> getNotificationInfo( | ||
@AuthenticationPrincipal PrincipalDetails principalDetails) { | ||
NotificationInfoResponses response = notificationService.getNotificationInfoById( | ||
principalDetails.getId()); | ||
|
||
return ResponseEntity.ok(response); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/example/jolvre/notification/dto/NotificationDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.jolvre.notification.dto; | ||
|
||
|
||
import com.example.jolvre.notification.entity.NotificationType; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class NotificationDTO { | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public static class NotificationInfoResponse { | ||
private String message; | ||
private NotificationType notificationType; | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public static class NotificationInfoResponses { | ||
List<NotificationInfoResponse> notificationInfoResponses; | ||
} | ||
|
||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/jolvre/notification/entity/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.jolvre.notification.entity; | ||
|
||
import com.example.jolvre.common.entity.BaseTimeEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Entity | ||
@NoArgsConstructor | ||
public class Notification extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "notification_id") | ||
private Long id; | ||
|
||
private String message; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private NotificationType notificationType; | ||
|
||
private Long receiverId; | ||
|
||
@Builder | ||
public Notification(String message, NotificationType notificationType, Long receiverId) { | ||
this.message = message; | ||
this.notificationType = notificationType; | ||
this.receiverId = receiverId; | ||
} | ||
} |
Oops, something went wrong.