Spring Boot 2.0.0.M6中的Spring调度

时间:2017-12-01 08:28:55

标签: spring spring-mvc spring-boot scheduled-tasks scheduling

我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件生成了一个Spring Boot Web应用程序。

使用的技术:

Spring Boot 2.0.0.M6,Java 8,maven。

我已创建此课程

com.iberia.task.scheduled

    public class IberiaAssetHandlerJob {


        private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        @Scheduled(cron = "0 * * * * ?")
        public void reportCurrentTime() {

            System.out.println("The time is now {}" + dateFormat.format(new Date()));

        }
    }

希望每分钟都能看到这条消息

和我的主要班级

@SpringBootApplication
@EnableScheduling
@Import({SecurityConfig.class})
public class IberiaWebUtilsApplication {

    public static void main(String[] args) {
        SpringApplication.run(IberiaWebUtilsApplication.class, args);
    }
}

但我在控制台中看不到任何消息

2 个答案:

答案 0 :(得分:0)

您应该使用Logger更改System.out.println(...)以查看消息,并在其答案中将类声明为Spring g00glen00b状态。

private static final Logger log = Logger.getLogger(IberiaAssetHandlerJob.class);

log.info("The time is now {}" + dateFormat.format(new Date()));

答案 1 :(得分:0)

-(void)callingUploadBusinessBussinessGalleryImageApi{ BusinessUser *busUser = [BusinessUser getBusinessUser]; NSMutableArray *arrImgData = [[NSMutableArray alloc] init]; for (int i = 0; i < self.chosenImages.count; i++) { uploadImageData = UIImageJPEGRepresentation([self.chosenImages objectAtIndex:i], 0.1); [arrImgData insertObject:uploadImageData atIndex:i]; } NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arrImgData]; //NSData *data =[NSPropertyListSerialization dataWithPropertyList:arrImgData format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil]; NSURL *uploadProfPicUrl = [NSURL URLWithString:BaseUrl]; NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init]; [dataDictionary setValue:[NSNumber numberWithLong:busUser.business_id] forKey:@"business_id"]; NetworkHandler *networkHandler = [[NetworkHandler alloc] initWithRequestUrl:uploadProfPicUrl withBody:dataDictionary withMethodType:HTTPMethodPOST withHeaderFeild:nil]; [networkHandler startUploadRequest:@"DirectoryProfilePhoto" withData:data withType:fileTypeJPGImage withUrlParameter:uploadBusinessGalleryImageUrl withFileLocation:@"imageFile" SuccessBlock:^(id responseObject) { NSLog(@"%@", responseObject); } ProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { } FailureBlock:^(NSString *errorDescription, id errorResponse) { }]; } 注释仅适用于Spring托管bean。您没有在SELECT t1.Country,t2.* FROM table_1 as t1, table_2 as t2 where t1.id='1' and t2.id='2' 之上添加@Scheduled注释,也没有手动创建bean(使用@Component注释)。

另外,请注意IberiaAssetHandlerJob@Bean使用的基础类)isn't necessarily thread-safe,所以如果您还有其他线程写入PrintStream,那么您可以得到奇怪的结果,这就是为什么建议使用日志框架(如答案中提到的lzagkaretos)。这也允许您使用System.out之类的占位符,例如:

System.out

确保使用多个参数,而不是在这种情况下将所有内容连接到单个字符串。