-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
154 additions
and
9 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
33 changes: 33 additions & 0 deletions
33
src/main/java/LearnMate/dev/common/config/AsyncConfig.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,33 @@ | ||
package LearnMate.dev.common.config; | ||
|
||
import LearnMate.dev.common.exception.CustomAsyncUncaughtExceptionHandler; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(5); | ||
executor.setMaxPoolSize(5); | ||
executor.setQueueCapacity(5); | ||
executor.setKeepAliveSeconds(30); | ||
executor.setThreadNamePrefix("async-executor-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return new CustomAsyncUncaughtExceptionHandler(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/LearnMate/dev/common/config/GoogleNaturalLanguageConfig.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 LearnMate.dev.common.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.language.v1.LanguageServiceSettings; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.io.IOException; | ||
|
||
@Configuration | ||
public class GoogleNaturalLanguageConfig { | ||
@Value("classpath:nlp.json") | ||
Resource gcsCredentials; | ||
|
||
@Bean | ||
public LanguageServiceSettings languageServiceSettings() { | ||
try { | ||
return LanguageServiceSettings.newBuilder() | ||
.setCredentialsProvider(() -> | ||
GoogleCredentials.fromStream(gcsCredentials.getInputStream())) | ||
.build(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to initialize LanguageServiceSettings", e); | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...va/LearnMate/dev/common/ApiException.java → ...te/dev/common/exception/ApiException.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
17 changes: 17 additions & 0 deletions
17
src/main/java/LearnMate/dev/common/exception/CustomAsyncUncaughtExceptionHandler.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,17 @@ | ||
package LearnMate.dev.common.exception; | ||
|
||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class CustomAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler { | ||
@Override | ||
public void handleUncaughtException(Throwable ex, Method method, Object... params) { | ||
System.out.println("Exception message - " + ex.getMessage()); | ||
System.out.println("Method name - " + method.getName()); | ||
|
||
for (Object param : params) { | ||
System.out.println("Parameter value - " + param); | ||
} | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...LearnMate/dev/common/ExceptionAdvice.java → ...dev/common/exception/ExceptionAdvice.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package LearnMate.dev.service; | ||
|
||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Service | ||
public class ActionTipService { | ||
|
||
@Async | ||
public CompletableFuture<String> getActionTip(String content) { | ||
String text = "Action Tip"; | ||
|
||
return CompletableFuture.completedFuture(text); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/LearnMate/dev/service/NaturalLanguageService.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,37 @@ | ||
package LearnMate.dev.service; | ||
|
||
import LearnMate.dev.common.exception.ApiException; | ||
import LearnMate.dev.common.ErrorStatus; | ||
import com.google.cloud.language.v1.Document; | ||
import com.google.cloud.language.v1.LanguageServiceClient; | ||
import com.google.cloud.language.v1.Sentiment; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Service | ||
public class NaturalLanguageService { | ||
|
||
/* | ||
* cloud Natural Language API를 호출해 텍스트에 대한 감정을 분석 | ||
* @param text | ||
* @return | ||
*/ | ||
@Async | ||
public CompletableFuture<Float> analyzeEmotion(String text) { | ||
try (LanguageServiceClient language = LanguageServiceClient.create()) { | ||
|
||
Document doc = Document.newBuilder().setContent(text).setType(Document.Type.PLAIN_TEXT).build(); | ||
Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment(); | ||
|
||
System.out.printf("Text: %s%n", text); | ||
System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude()); | ||
|
||
return CompletableFuture.completedFuture(sentiment.getScore()); | ||
} catch (IOException e) { | ||
throw new ApiException(ErrorStatus._ANALYZE_EMOTION_ERROR); | ||
} | ||
} | ||
} |
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