Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/example/SucceSS/SucceSsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableJpaAuditing
@EnableScheduling
@SpringBootApplication
public class SucceSsApplication {

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/example/SucceSS/config/chat/SchedulerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.SucceSS.config.chat;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
public class SchedulerConfig {
@Bean
public ThreadPoolTaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
scheduler.setThreadNamePrefix("ChatRoomDeletingScheduler-");
scheduler.initialize();
return scheduler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class ChatRoomService {

private final ChatRoomRepository chatRoomRepository;
private final ChatRepository chatRepository;
private final ConcurrentHashMap<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();
private final ThreadPoolTaskScheduler taskScheduler;


@Transactional
public ChatRoomResponseDto createChatRoom(Member member) {
Expand All @@ -29,9 +39,8 @@ public ChatRoomResponseDto createChatRoom(Member member) {
.title("New Chat")
.build();

chatRoomRepository.save(chatRoom);

//sendFirstMessage(chatRoom);
chatRoom = chatRoomRepository.save(chatRoom);
scheduleDeletion(chatRoom.getChatRoomId());
return chatRoom.toResponse();
}

Expand All @@ -47,15 +56,6 @@ public Page<ChatResponseDto> getChatPages(Long chatRoomId, Pageable pageable) {
.map(ChatResponseDto::fromWithoutLocation);
}

/*
private void sendFirstMessage(ChatRoom chatRoom) {
ChatDto dto = ChatDto.toChatDto(chatRoom.getChatRoomId(),chatRoom.getMemberId(), "New Chat", "CHAT");
producer.sendMessageToUser(dto);

chatRepository.save(Chat.of(dto));
}
*/

@Transactional(readOnly = true)
public Page<ChatRoomResponseDto> getChatRoomPages(Member member, Pageable pageable) {
return chatRoomRepository.getPagesByMemberId(member.getId()
Expand All @@ -67,4 +67,22 @@ private Pageable getChatRoomPageableWithSort(Pageable pageable) {
, Sort.by(Sort.Direction.DESC,"updatedAt"));
}

public void scheduleDeletion(Long chatRoomId) {
ScheduledFuture<?> future = taskScheduler.schedule(
() -> deleteChatRoom(chatRoomId),
Instant.now().plusSeconds(5*60)
);
scheduledTasks.put(chatRoomId, future);
}

public void cancelScheduledDeletion(Long chatRoomId) {
ScheduledFuture<?> future = scheduledTasks.get(chatRoomId);
if (future != null && !future.isDone()) {
future.cancel(false);
}
scheduledTasks.remove(chatRoomId);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ChatService {
private final ChatRoomRepository chatRoomRepository;
private static final String USER = "user";
private static final String LUMI = "lumi";
private final ChatRoomService chatRoomService;

@Value("${spring.huggingface.api.url}")
private String API_URL;
Expand Down Expand Up @@ -160,5 +161,8 @@ public void updateChatRoomInfos(ChatRoom chatRoom, ChatRequestDto chatDto) {
chatRoom.updateChatRoomTitle(chatDto.getText());
}
chatRoom.setUpdatedDate(LocalDateTime.now());

// 삭제 스케줄링 취소
chatRoomService.cancelScheduledDeletion(chatRoom.getChatRoomId());
}
}