--- title: '在windows主機 run jar file by ApplicationRunner with switch cases' disqus: hackmd --- ###### tags: `SpringBoot` 在windows主機 run jar file by ApplicationRunner with switch cases === [TOC] ## 適用場景 你有一個專案想要run,而且這個專案使用ApplicationRunner來取代傳統的spring boot web app,也就是說 可以使用 switch cases ```java= package com.allproduct.aicenter.robin.applicationmodelcenter; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.retry.annotation.EnableRetry; import com.allproduct.aicenter.robin.applicationmodelcenter.alarm.service.GoogleAccountAllieParticipationRateService; import com.allproduct.aicenter.robin.applicationmodelcenter.properties.ApplicationProperties; import com.allproduct.aicenter.robin.applicationmodelcenter.test.TestAccountSponsorService; import com.allproduct.aicenter.robin.applicationmodelcenter.test.TestAllieService; import com.allproduct.aicenter.robin.applicationmodelcenter.test.TestGroupPermissionsService; import com.allproduct.aicenter.robin.applicationmodelcenter.test.TestMailHTMLContentBuilderService; import com.allproduct.aicenter.robin.businessmodelcenter.ps.pojo.AccountOwnerInfoPojo; import com.allproduct.aicenter.robin.businessmodelcenter.ps.pojo.EmployeeOwnAccountAndGroupInfoPojo; import com.allproduct.aicenter.robin.businessmodelcenter.ps.pojo.GroupManagerInfoPojo; import com.allproduct.aicenter.robin.businessmodelcenter.ps.service.NotificationEmailService; import com.allproduct.aicenter.robin.databaseentitydummy_db_sys_used.Configuration.DummyDbSysUsedDataSourceConfiguration; import com.allproduct.aicenter.robin.toolkitcommonlibrary.ApplicationArguments.SpringbootApplicationArgumentsToolkit; import com.allproduct.aicenter.robin.toolkitcommonlibrary.ApplicationArguments.Bean.ServiceCommandArgumentsBean; import com.allproduct.aicenter.robin.toolkitcommonlibrary.MailService.EmailService; import com.allproduct.aicenter.robin.toolkitcommonlibrary.String.StringToolkit; @EnableRetry @SpringBootApplication @PropertySource({"classpath:db-${spring.profiles.active}.properties"}) @ComponentScan(basePackages = { "com.allproduct.aicenter" }, excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {}) ) public class ApplicationModelCenterApplication implements ApplicationRunner { @Autowired private EmailService emailService; @Autowired private ApplicationProperties applicationProperties; /* * Production */ // @Autowired // private CampaignLowSpendRatioDetectService campaignLowSpendRatioDetectService; // // @Autowired // private CampaignPerformance30d60d90dReportService campaignPerformance30d60d90dReportService; // // @Autowired // private AccountBudgetGoingRunoutAlarmService accountBudgetGoingRunoutAlarmService; // // @Autowired // private BudgetAndSpendingAbnormalAlarmService budgetAndSpendingAbnormalAlarmService; // // @Autowired // private UserListSizeAbnormalAlarmService userListSizeAbnormalAlarmService; // // @Autowired // private NewbornAdsPerformanceReportService newbornAdsPerformanceReportService; // // @Autowired // private ConversionActionAbnormalAlarmService conversionActionAbnormalAlarmService; // // @Autowired // private AccountAboutAIStrategySuggestionService accountAboutAIStrategySuggestionService; @Autowired private GoogleAccountAllieParticipationRateService googleAccountAllieParticipationRateService; // /* // * Health Check // */ // @Autowired // private RestServiceHealthyCheckAgent restServiceHealthyCheckAgent; /* * Dev-test */ @Autowired private TestAllieService testAllieService; @Autowired private NotificationEmailService permissionSystemNotificationEmailService; // @Autowired // private GroupService permissionSystemGroupService; @Autowired private TestAccountSponsorService testAccountSponsorService; @Autowired private TestGroupPermissionsService testGroupPermissionsService; @Autowired private TestMailHTMLContentBuilderService testMailHTMLContentBuilderService; private static final Logger LOG = LoggerFactory.getLogger(ApplicationModelCenterApplication.class); private static final String APPLICATION_NAME = ApplicationModelCenterApplication.class.getSimpleName(); public static void main(String[] args) { LOG.info("{} : Spring boot ApplicationRunner starting", APPLICATION_NAME); SpringApplication.run(ApplicationModelCenterApplication.class, args); LOG.info("{} : Spring boot ApplicationRunner stopped", APPLICATION_NAME); } @Override public void run(ApplicationArguments args) throws Exception { LOG.info("{} run() start", APPLICATION_NAME); ServiceCommandArgumentsBean serviceCommandArgumentsbean = SpringbootApplicationArgumentsToolkit.processArguments(args); String assignServiceName = serviceCommandArgumentsbean.getAssignService(); try { LOG.info("Execute: {}...Start", assignServiceName); switch (serviceCommandArgumentsbean.getAssignService().toUpperCase()) { // case "ALARM_CAMPAIGN_LOW_SPEND_RATIO_DETECT_SERVICE": // campaignLowSpendRatioDetectService.run(serviceCommandArgumentsbean); // break; // // case "ALARM_ACCOUNT_BUDGET_GOING_RUN_OUT_SERVICE": // accountBudgetGoingRunoutAlarmService.run(); // break; // // case "ALARM_BUDGET_AND_SPENDING_ABNORMAL_SERVICE": // budgetAndSpendingAbnormalAlarmService.run(); // break; // // case "REPORT_ALLIE_CAMPAIGN_PERFORMANCE_SERVICE": // campaignPerformance30d60d90dReportService.run(); // break; // // case "ALARM_USER_LIST_SIZE_ABNORMAL_SERVICE": // userListSizeAbnormalAlarmService.run(); // break; // // case "REPORT_NEWBORN_ADS_PERFORMANCE_SERVICE": // newbornAdsPerformanceReportService.run(); // break; // // // case "ALARM_CONVERSION_ACTION_ABNORMAL_SERVICE": // conversionActionAbnormalAlarmService.run(); // break; // // // case "REPORT_AI_STRATEGY_SUGGESTION_SERVICE": // accountAboutAIStrategySuggestionService.run(); // break; case "REPORT_GOOGLE_ACCOUNT_ALLIE_PARTICIPATION_RATE_SERVICE": googleAccountAllieParticipationRateService.run(); break; // case "REST_SERVICE_HEALTH_CHECK_AGENT": // restServiceHealthyCheckAgent.run(); // break; case "SHOW_ALARM_RECEIVER": LOG.info("Application.alarmReceiver: {}", StringToolkit.JoinStringArrayItemTogether(applicationProperties.getAlarmReceiver(), ",")); break; case "TEST_ALLIE_SERVICE": testAllieService.runAllieService(); break; case "TEST_PERMISSION_SYSTEM_API_SERVICE": // String[] plateformStrings = {"Google","Facebook","Yahoo","Line","other"}; String[] plateformStrings = {"Google"}; List<EmployeeOwnAccountAndGroupInfoPojo> result = permissionSystemNotificationEmailService.getEmployeeAndAccountsAndGroupsInfoPojos("GOOGLE_NEWBORN_PER_REPORT", plateformStrings); // List<EmployeeOwnAccountAndGroupInfoPojo> result = permissionSystemGroupService.getEmployeeAndAccountsPojos("ai_center", plateformStrings); LOG.info("Total get {} <EmployeeOwnAccountAndGroupInfoPojo>", result.size()); // LOG.info(result.toString()); for (EmployeeOwnAccountAndGroupInfoPojo employeeOwnAccountAndGroupInfoPojo : result) { LOG.info("=======[id={}, name={}, email={}, role={}, isExternal={}]======", employeeOwnAccountAndGroupInfoPojo.getId(), employeeOwnAccountAndGroupInfoPojo.getName(), employeeOwnAccountAndGroupInfoPojo.getEmail(), employeeOwnAccountAndGroupInfoPojo.getRole(), employeeOwnAccountAndGroupInfoPojo.getIsExternal() ); LOG.info(" ==> Total get {} <AccountOwnerInfoPojoList>", employeeOwnAccountAndGroupInfoPojo.getAccountOwnerInfoPojoList().size()); for (AccountOwnerInfoPojo accountOwnerInfoPojo : employeeOwnAccountAndGroupInfoPojo.getAccountOwnerInfoPojoList()) { LOG.info(accountOwnerInfoPojo.toString()); } LOG.info(" ==> Total get {} <GroupManagerInfoPojoList>", employeeOwnAccountAndGroupInfoPojo.getGroupManagerInfoPojoList().size()); for (GroupManagerInfoPojo groupManagerInfoPojo : employeeOwnAccountAndGroupInfoPojo.getGroupManagerInfoPojoList()) { LOG.info(groupManagerInfoPojo.toString()); } } break; case "TEST_ACCOUNT_SPONSOR_SERVICE": testAccountSponsorService.runService(); break; case "TEST_GROUP_PERMISSIONS_SERVICE": testGroupPermissionsService.runService(); break; case "TEST_MAIL_HTML_CONTENT_BUILDER_SERVICE": testMailHTMLContentBuilderService.runService(); break; default: LOG.info("[Default]: {} is not defined or un-recognized will do nothing", assignServiceName); LOG.info("Execute: {}...Start", assignServiceName); break; } LOG.info("Execute: {}...End", assignServiceName); } catch (Exception e) { LOG.error(e.toString()); String[] toCc = null; emailService.sendMail("[Error] " + APPLICATION_NAME + " ==> Execute: " + assignServiceName, e.toString(), applicationProperties.getAlarmReceiver(), toCc); } LOG.info("{} run() end", APPLICATION_NAME); } private void testMailService() { LOG.info("do mail test...send mail start"); String[] toPrimary = new String[] {"robin@tradeserv.com"}; String[] toCc = new String[] {"ccEmail@tradeserv.com"}; emailService.sendMail("SpringMailServiceTest", "Content here...", toPrimary, toCc); LOG.info("do mail test...send mail completed"); } } ``` > key 我們想run的 service name 就在 case= 裡面 > 比如說我們想RUN testMailHTMLContentBuilderService 這個service > 那麼你就要使用TEST_MAIL_HTML_CONTENT_BUILDER_SERVICE ## 先備條件 已經打包好的jar file 對專案 run as maven build... goal=package ![](https://i.imgur.com/Lp51kwH.png) > 之後跑到C:\Users\TDG_USER\workspaceforv8\Google-AI-Reward\target\ ![](https://i.imgur.com/2AcIlpa.png) 最後打上 ```java= java -jar Google-AI-Reward-0.0.1-SNAPSHOT.jar --SERVICE=TEST_MAIL_HTML_CONTENT_BUILDER_SERVICE ```