Skip to content

Commit 4c75aa5

Browse files
authored
Merge pull request #25 from dai0304/feature/improve-notification-service
Improve NotoficationService
2 parents 9bbcd7a + d5d2adb commit 4c75aa5

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

spar-wings-sns-notification/src/main/java/jp/xet/sparwings/aws/sns/NotificationService.java

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import java.util.Locale;
2323
import java.util.Map;
2424

25+
import lombok.RequiredArgsConstructor;
26+
import lombok.extern.slf4j.Slf4j;
27+
2528
import org.springframework.beans.factory.InitializingBean;
2629
import org.springframework.beans.factory.annotation.Autowired;
2730
import org.springframework.beans.factory.annotation.Value;
2831

29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
3132
import org.slf4j.MDC;
3233

3334
import com.amazonaws.services.sns.AmazonSNS;
@@ -45,11 +46,10 @@
4546
* @since 0.3
4647
* @author daisuke
4748
*/
49+
@Slf4j
50+
@RequiredArgsConstructor
4851
public class NotificationService implements InitializingBean {
4952

50-
private static Logger logger = LoggerFactory.getLogger(NotificationService.class);
51-
52-
5353
/**
5454
* Returns stacktrace as string.
5555
*
@@ -67,10 +67,11 @@ private static String toString(Throwable t) {
6767
}
6868

6969

70+
private final AmazonSNS sns;
71+
7072
private final String appCodeName;
7173

72-
@Autowired
73-
AmazonSNS sns;
74+
private final EnvironmentService env;
7475

7576
@Autowired(required = false)
7677
InstanceInfo instanceInfo;
@@ -79,33 +80,44 @@ private static String toString(Throwable t) {
7980
@Autowired(required = false)
8081
jp.xet.sparwings.aws.ec2.InstanceMetadata instanceMetadata;
8182

82-
@Autowired
83-
EnvironmentService env;
84-
83+
@Deprecated
8584
@Value("#{systemEnvironment['CFN_STACK_NAME'] ?: systemProperties['CFN_STACK_NAME']}")
86-
String stackName;
85+
String deprecatedStackName;
8786

87+
@Deprecated
8888
@Value("#{systemEnvironment['DEV_TOPIC_ARN'] ?: systemProperties['DEV_TOPIC_ARN']}")
89-
String devTopicArn;
89+
String deprecatedDevTopicArn;
9090

91+
@Deprecated
9192
@Value("#{systemEnvironment['OPS_TOPIC_ARN'] ?: systemProperties['OPS_TOPIC_ARN']}")
93+
String deprecatedOpsTopicArn;
94+
95+
@Value("${sparwings.notification.stack-name}")
96+
String stackName;
97+
98+
@Value("${sparwings.notification.dev}")
99+
String devTopicArn;
100+
101+
@Value("${sparwings.notification.ops}")
92102
String opsTopicArn;
93103

94104

95-
/**
96-
* インスタンスを生成する。
97-
*
98-
* @param appCodeName Application code name
99-
* @since 0.3
100-
*/
101-
public NotificationService(String appCodeName) {
102-
this.appCodeName = appCodeName;
105+
private String getStackName() {
106+
return stackName != null ? stackName : deprecatedStackName;
107+
}
108+
109+
private String getDevTopicArn() {
110+
return devTopicArn != null ? devTopicArn : deprecatedDevTopicArn;
111+
}
112+
113+
private String getOpsTopicArn() {
114+
return opsTopicArn != null ? opsTopicArn : deprecatedOpsTopicArn;
103115
}
104116

105117
@Override
106118
public void afterPropertiesSet() {
107-
logger.info("Initialize devTopicArn = {}", devTopicArn);
108-
logger.info("Initialize opsTopicArn = {}", opsTopicArn);
119+
log.info("Initialize devTopicArn = {}", getDevTopicArn());
120+
log.info("Initialize opsTopicArn = {}", getOpsTopicArn());
109121
}
110122

111123
/**
@@ -116,7 +128,7 @@ public void afterPropertiesSet() {
116128
* @since 0.3
117129
*/
118130
public void notifyOps(String subject, String message) {
119-
notifyMessage0(opsTopicArn, subject, message);
131+
notifyMessage0(getOpsTopicArn(), subject, message);
120132
}
121133

122134
/**
@@ -189,7 +201,7 @@ public void notifyDev(String subject, Map<String, String> messageMap, Throwable
189201
messageMap.put("stackTrace", toString(t));
190202
}
191203

192-
notifyMessage0(devTopicArn, subject, createMessage(messageMap));
204+
notifyMessage0(getDevTopicArn(), subject, createMessage(messageMap));
193205
}
194206

195207
private String createMessage(Map<String, String> messageMap) {
@@ -208,25 +220,25 @@ private String createMessage(Map<String, String> messageMap) {
208220

209221
private void notifyMessage0(String topicArn, String originalSubject, String message) {
210222
String subject = String.format(Locale.ENGLISH, "[%s:%s] %s (%s)",
211-
appCodeName, stackName, originalSubject, env.getActiveProfilesAsString());
223+
appCodeName, getStackName(), originalSubject, env.getActiveProfilesAsString());
212224
if (subject.length() > 100) {
213-
logger.warn("Topic message subject is truncated. Full subject is: {}", subject);
225+
log.warn("Topic message subject is truncated. Full subject is: {}", subject);
214226
subject = subject.substring(0, 100);
215227
}
216228

217-
logger.debug("notify message to topic[{}] - {} : {}", topicArn, subject, message);
218-
if (topicArn == null || topicArn.isEmpty() || topicArn.equals("arn:aws:sns:null")) {
219-
logger.debug("topicArn: NULL");
229+
log.debug("notify message to topic[{}] - {} : {}", topicArn, subject, message);
230+
if (sns == null || topicArn == null || topicArn.isEmpty() || topicArn.equals("arn:aws:sns:null")) {
231+
log.debug("topicArn: NULL");
220232
return;
221233
}
222234
try {
223235
sns.publish(new PublishRequest()
224236
.withTopicArn(topicArn)
225237
.withSubject(subject)
226238
.withMessage(message));
227-
logger.debug("SNS Notification published: {} - {}", topicArn, subject);
239+
log.debug("SNS Notification published: {} - {}", topicArn, subject);
228240
} catch (Exception e) { // NOPMD
229-
logger.error("SNS Publish failed: {} - {} - {}", topicArn, subject, message, e);
241+
log.error("SNS Publish failed: {} - {} - {}", topicArn, subject, message, e);
230242
}
231243
}
232244
}

0 commit comments

Comments
 (0)