The log file exists and logs are generated but it isn't logging Processing index request in the logs.
Is the code correct? When index page is accessed the thymleaf content "DevOps" is being retrieved and displayed in index.html but no logs from logger.info in the logs.
package com.example.devops.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class WelcomeController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
#RequestMapping("/")
public String welcome(Model model) {
logger.info("Processing index request");
model.addAttribute("course", "DevOps");
return "index";
}
}
Log entires:
021-08-27 18:41:08.557 [INFO ] [localhost-startStop-1] c.e.d.ServletInitializer - Starting ServletInitializer v1.3.1-SNAPSHOT using Java 1.8.0_301 on DESKTOP-6O551TV with PID 23756 (F:\Devops\apache-tomcat-8.5.70\webapps\devops\WEB-INF\classes started by user in F:\Devops\apache-tomcat-8.5.70\bin)
2021-08-27 18:41:08.615 [INFO ] [localhost-startStop-1] c.e.d.ServletInitializer - The following profiles are active: dev
2021-08-27 18:41:09.573 [INFO ] [localhost-startStop-1] o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 847 ms
2021-08-27 18:41:10.215 [INFO ] [localhost-startStop-1] o.s.b.a.w.s.WelcomePageHandlerMapping - Adding welcome page template: index
2021-08-27 18:41:10.374 [INFO ] [localhost-startStop-1] c.e.d.ServletInitializer - Started ServletInitializer in 2.416 seconds (JVM running for 11.278)
Actually its working as expected. application-dev.properties file did not have full path of the log and a couple of commits were missing.
It is working.
Related
I was trying to create my basic spring application and try to connect to my MongoDB database.
so a create my entity
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document
#Data #AllArgsConstructor #NoArgsConstructor #ToString
public class MyProduct {
#Id
private String id;
private String name;
private int price;
}
my repository
import com.nizar.back.demo.entities.MyProduct;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
#RepositoryRestResource
public interface MyProductRepository extends MongoRepository<MyProduct,String> {
}
my application.properties
server.port=8093
spring.data.mongodb.uri=mongodb://<my username>:<my password>#cluster0.zgrrb.mongodb.net/appDatabase?retryWrites=true&w=majority
and this is my application class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
#SpringBootApplication
#EnableAutoConfiguration(exclude={ DataSourceAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
what i notice that i have this error when i try to run my application, and i didn't understant why ?
2021-07-21 18:04:49.921 INFO 7524 --- [ main] com.nizar.back.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_291 on DESKTOP-6PB4U74 with PID 7524 (C:\dev projects\spring project\demo\target\classes started by abdel in C:\dev projects\spring project\demo)
2021-07-21 18:04:49.924 INFO 7524 --- [ main] com.nizar.back.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-07-21 18:04:50.411 INFO 7524 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2021-07-21 18:04:50.459 INFO 7524 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 43 ms. Found 1 MongoDB repository interfaces.
2021-07-21 18:04:50.724 WARN 7524 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myProductRepository' defined in com.nizar.back.demo.dao.MyProductRepository defined in #EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available
2021-07-21 18:04:50.732 INFO 7524 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-07-21 18:04:50.749 ERROR 7524 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'mongoTemplate' that could not be found.
I just did this project to learn about spring boot. and i just need to find why i have this error and how i can solve it
it didn't choose the right version of the driver.
so I change the drive version on https://cloud.mongodb.com/ to 3.4 or later and it give me an other url to connect and it work
mongodb://<MyUsername>:<Mypassword>#cluster0-shard-00-00.zgrrb.mongodb.net:27017,cluster0-shard-00-01.zgrrb.mongodb.net:27017,cluster0-shard-00-02.zgrrb.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-sr24zl-shard-0&authSource=admin&retryWrites=true&w=majority
Building a separate Web Client and Rest Service in Spring Boot. Making two separate apps. Trying to make an AJAX call from the client on http://localhost:8081/ and hit the rest service on http://localhost:8080/someURL/invokeMethod which should invoke the rest service controller method and make a LOGGER.info() out to the console. When I launch the page at http://localhost:8081/ the AJAX on the client should make a call to the service however I am unable to get a log out to the console from the controller getLogOut() method on the rest service controller.
Rest Service Controller
package com.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/someURL")
public class ServiceController {
private static final Logger LOGGER = LoggerFactory.getLogger(SearchController.class.getName());
#GetMapping("/invokeMethod")
public void getLogOut() {
LOGGER.info("/n/n/n/n/n***************LOGGER IN getLogOut************");
}
}
Web Client index.html :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "http://localhost:8080/someURL/invokeMethod"
});
});
</script>
</head>
<body>
<h1>Load Page to make ajax call</h1>
</body>
</html>
Web Client Controller :
package com.demo.Web.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping
public class WebController {
private static final Logger LOGGER = LoggerFactory.getLogger(WebController.class.getName());
#GetMapping("/")
public String viewPage() {
LOGGER.info("\n\n\n**********In Index**********");
return "index";
}
}
Not getting errors from the console. Just simply getting from console :
0:0:0:0:0:0:0:1 - - [06/Oct/2019:21:21:28 -0500] "GET /someURL/invokeMethod HTTP/1.1" 200 722
Not sure what above from console is trying tell me.
Stacktrace (should be complete)
[INFO]~2019-10-07-09.53.05.313CDT~~~~~~ c.e.h.RestServiceApplication Starting RestServiceApplication on 5CG9107HK6 with PID 162992
[INFO]~2019-10-07-09.53.05.317CDT~~~~~~ c.e.h.RestServiceApplication No active profile set, falling back to default profiles: default
[INFO]~2019-10-07-09.53.05.543CDT~~~~~~ o.s.b.d.e.DevToolsPropertyDefaultsPostProcessor Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[INFO]~2019-10-07-09.53.05.544CDT~~~~~~ o.s.b.d.e.DevToolsPropertyDefaultsPostProcessor For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[INFO]~2019-10-07-09.53.06.685CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$6e19173b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.06.692CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.06.694CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler#cb177' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.06.701CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$92edb9ed] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.06.706CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'org.springframework.security.config.annotation.method.configuration.Jsr250MetadataSourceConfiguration' of type [org.springframework.security.config.annotation.method.configuration.Jsr250MetadataSourceConfiguration$$EnhancerBySpringCGLIB$$b842d203] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.06.707CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'jsr250MethodSecurityMetadataSource' of type [org.springframework.security.access.annotation.Jsr250MethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.06.708CDT~~~~~~ o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[INFO]~2019-10-07-09.53.07.074CDT~~~~~~ o.s.b.w.e.t.TomcatWebServer Tomcat initialized with port(s): 8080 (http)
[INFO]~2019-10-07-09.53.07.092CDT~~~~~~ o.a.c.h.Http11NioProtocol Initializing ProtocolHandler ["http-nio-8080"]
[INFO]~2019-10-07-09.53.07.104CDT~~~~~~ o.a.c.c.StandardService Starting service [Tomcat]
[INFO]~2019-10-07-09.53.07.105CDT~~~~~~ o.a.c.core.StandardEngine Starting Servlet engine: [Apache Tomcat/9.0.26]
[INFO]~2019-10-07-09.53.07.225CDT~~~~~~ o.a.c.c.C.[.[.[/] Initializing Spring embedded WebApplicationContext
[INFO]~2019-10-07-09.53.07.225CDT~~~~~~ o.s.w.c.ContextLoader Root WebApplicationContext: initialization completed in 1681 ms
[INFO]~2019-10-07-09.53.07.962CDT~~~~~~ n.r.s.b.l.a.LogbackAccessContext Configured the Logback-access: context=[default], config=[classpath:logback-access.xml]
[INFO]~2019-10-07-09.53.08.093CDT~~~~~~ o.s.s.c.ThreadPoolTaskExecutor Initializing ExecutorService 'applicationTaskExecutor'
[INFO]~2019-10-07-09.53.08.334CDT~~~~~~ o.s.s.w.DefaultSecurityFilterChain Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#1c48ba2, org.springframework.security.web.context.SecurityContextPersistenceFilter#c57134, org.springframework.security.web.header.HeaderWriterFilter#1248239, org.springframework.security.web.csrf.CsrfFilter#183a2cf, org.springframework.security.web.authentication.logout.LogoutFilter#159fde7, com.edwardjones.framework.security.spring.wam.WamAuthenticationFilter#146ee78, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#261552, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter#e0f96e, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter#300ae4, org.springframework.security.web.authentication.www.BasicAuthenticationFilter#44ff58, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#1de5909, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#15d7b9c, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#df7a4d, org.springframework.security.web.session.SessionManagementFilter#348fee, org.springframework.security.web.access.ExceptionTranslationFilter#13719ad, org.springframework.security.web.access.ExceptionTranslationFilter#b78775, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#1f01a41]
[INFO]~2019-10-07-09.53.08.807CDT~~~~~~ o.s.s.c.ThreadPoolTaskScheduler Initializing ExecutorService 'taskScheduler'
[INFO]~2019-10-07-09.53.08.836CDT~~~~~~ o.s.b.d.a.OptionalLiveReloadServer LiveReload server is running on port 35729
[INFO]~2019-10-07-09.53.08.843CDT~~~~~~ o.s.b.a.e.w.EndpointLinksResolver Exposing 2 endpoint(s) beneath base path '/actuator'
[INFO]~2019-10-07-09.53.08.889CDT~~~~~~ o.a.c.h.Http11NioProtocol Starting ProtocolHandler ["http-nio-8080"]
[INFO]~2019-10-07-09.53.08.996CDT~~~~~~ o.s.b.w.e.t.TomcatWebServer Tomcat started on port(s): 8080 (http) with context path ''
[INFO]~2019-10-07-09.53.09.001CDT~~~~~~ c.e.h.RestServiceApplication Started RestServiceApplication in 3.959 seconds (JVM running for 4.804)
[INFO]~2019-10-07-09.53.39.001CDT~~~~~~ o.a.c.c.C.[.[.[/] Initializing Spring DispatcherServlet 'dispatcherServlet'
[INFO]~2019-10-07-09.53.39.001CDT~~~~~~ o.s.w.s.DispatcherServlet Initializing Servlet 'dispatcherServlet'
[INFO]~2019-10-07-09.53.39.008CDT~~~~~~ o.s.w.s.DispatcherServlet Completed initialization in 7 ms
0:0:0:0:0:0:0:1 - - [07/Oct/2019:09:53:39 -0500] "GET /someURL/invokeMethod HTTP/1.1" 200 722
I just wrote a Springboot application as a testing Webserver which origins from spring.io.
It packaged initially in "jar", changed it to "war" file.
And get the application class code as below:
package com.ronzhong;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.context.WebApplicationContext;
#SpringBootApplication(scanBasePackages={"com.ronzhong.libraryManagement"})
public class LibraryManagementApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
application.application().setBannerMode(Mode.OFF);
return application.sources(SpringApplicationBuilder.class);
}
protected WebApplicationContext run(SpringApplication application) {
return (WebApplicationContext) application.run();
}
// public static void main(String[] args) {
// SpringApplication.run(LibraryManagementApplication.class, args);
//}
}
and the controller is :
package com.ronzhong.libraryManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.*;
//#CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
#RestController
#RequestMapping("/api")
public class CounterController {
#Autowired
private BookService bookService;
#PostMapping
public boolean add(#RequestBody Book book){
return bookService.add(book);
}
#RequestMapping(value = "/books", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Book[] get(){
return bookService.getBooks();
}
#RequestMapping(value = "{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Book findOne(#PathVariable("id") int id){
return bookService.findById(id);
}
}
And I also checked the
"Dynamic Web Module" in the project facets.
Finally, I got these logs when launch tomcat server with this war file generated.
INFO: 2 Spring WebApplicationInitializers detected on classpath
2018-11-28 10:43:31.925 INFO 14888 --- [ main] c.ronzhong.LibraryManagementApplication : Starting LibraryManagementApplication on ronzhongmachine with PID 14888 (C:\Users\ronzhong\workspace_testserver\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\library-Management\WEB-INF\classes started by ronzhong in C:\tools_installers\eclipse-jee-2018-09-win32-x86_64\eclipse)
2018-11-28 10:43:32.002 INFO 14888 --- [ main] c.ronzhong.LibraryManagementApplication : No active profile set, falling back to default profiles: default
2018-11-28 10:43:33.910 INFO 14888 --- [ main] o.a.c.c.C.[.[.[/library-Management] : Initializing Spring embedded WebApplicationContext
2018-11-28 10:43:33.911 INFO 14888 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1604 ms
2018-11-28 10:43:34.362 INFO 14888 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*]
2018-11-28 10:43:34.704 INFO 14888 --- [ main] c.ronzhong.LibraryManagementApplication : Started LibraryManagementApplication in 5.958 seconds (JVM running for 36.407)
2018-11-28 10:43:34.955 INFO 14888 --- [ main] org.apache.coyote.ajp.AjpNioProtocol : Starting ProtocolHandler ["ajp-nio-8009"]
2018-11-28 10:43:34.968 INFO 14888 --- [ main] org.apache.catalina.startup.Catalina : Server startup in 28177 ms
There're no mappings info in the logs.
And if I request with http://localhost:8080/api/books, it will get 404 error.
But the logs hasn't change even a little.
So I googled the potential causes, and excluded those causes temporarily:
springboot application does not scan the package which controller locates?
I moved springboot application class to the uplevel package, and also specified what kind of package it should scan.
While you see, it doesn't work.
the annotation issue?
Some people say we should use #RestController annotation instead of #Controller, better add the #ResponseBody, while I did.
it does't work, either.
the deployment issue?
don't know, I did changed the build path of this project, which generated jar file and war file normally, but webapp folder is empty, since I don't have any static resource, I think it's fine, right?
Anyone can help me? very appreciate for your answer, thanks in advance.
From the logs, I can see that your war file name is library-Management, whichh means its deloyed under this folder, as logs states. So you should check http://localhost:8080/library-Management/api/books instead.
You get 404 from Tomcat, it doesnt hit your app at all, this is ehy you dont see any logs.
Why does Spring execute my custom #Cacheable keyGenerator twice for a single invocation of a method annotated #Cacheable, why not do it just once.
My KeyGenerator implementation
package com.example.demo;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* Custom key generator
*/
#Component(value = "customerKeyGenerator")
public class CustomerKeyGen implements KeyGenerator
{
#Override
public Object generate(Object target, Method method, Object... params)
{
System.out.println("Generating a key");
ArrayList<String> customerNames = (ArrayList<String>) params[0];
return customerNames.hashCode();
}
}
My Method annotated with #Cacheable with custom keyGenerator
package com.example.demo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
#Component
public class CustomerService {
#Cacheable(value = "customersCache", keyGenerator = "customerKeyGenerator")
public int getCountOfCustomers(ArrayList<String> customerNames) {
return customerNames.size();
}
}
Spring Rest Controller which invokes the method annotated with #Cacheable
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
#Controller
public class CustomerController {
#Autowired
CustomerService customerService;
#RequestMapping("/")
#ResponseBody
String home() {
return "Hello World!";
}
#RequestMapping("/countCustomers")
#ResponseBody
String countCustomers() {
ArrayList<String> customerNames = new ArrayList<>();
customerNames.add("john");
customerNames.add("bill");
return "countOfCustomers=" + String.valueOf(customerService.getCountOfCustomers(customerNames));
}
}
When I make a single invocation of the method annotated with #Cacheable with my custom keyGenerator, I see 2 executions in my log and dubugger of
System.out.println("Generating a key");
Curl to trigger method call invocation
curl http://127.0.0.1:8080/countCustomers
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18 100 18 0 0 18 0 0:00:01 --:--:-- 0:00:01
76countOfCustomers=2
Log
I have the following setting in application properties to enable tracing of the cache
logging.level.org.springframework.cache=TRACE
...
2018-08-27 11:56:53.753 INFO 18756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-08-27 11:56:53.757 INFO 18756 --- [ main] c.example.demo.TestCacheableApplication : Started TestCacheableApplication in 3.543 seconds (JVM running for 5.137)
2018-08-27 11:56:58.411 INFO 18756 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-27 11:56:58.411 INFO 18756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-27 11:56:58.446 INFO 18756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 35 ms
Generating a key
2018-08-27 11:56:58.480 TRACE 18756 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : Computed cache key '104328221' for operation Builder[public int com.example.demo.CustomerService.getCountOfCustomers(java.util.ArrayList)] caches=[customersCache] | key='' | keyGenerator='customerKeyGenerator' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2018-08-27 11:56:58.480 TRACE 18756 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : No cache entry for key '104328221' in cache(s) [customersCache]
Generating a key
2018-08-27 11:56:58.480 TRACE 18756 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : Computed cache key '104328221' for operation Builder[public int com.example.demo.CustomerService.getCountOfCustomers(java.util.ArrayList)] caches=[customersCache] | key='' | keyGenerator='customerKeyGenerator' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
Conceptually, I would have thought Spring would need to run the keyGenerator only once, use it firstly to lookup the cache, and if not found, when the method is complete, use that same key to put to the cache. So I don't understand why I see this running twice.
My issues with this:
I am confused with how it works and why
The logging and debugging become confusing
Potential performance impact, even though keyGeneration implementation should be cheap, why do it multiple times.
I think I know why, key is generated once for lookup in cache, and once for putting into the cache. Not sure why it works that way, but appears to be what is happening.
I have created a Spring Boot application. I have configured my class that contains the scheduler method startService().
Below is my code :
Service Class :
package com.mk.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.mk.envers.model.BossExtChange;
import com.mk.envers.model.BossExtChangeRepository;
#Component
public class EnverseDemoService {
#Autowired
BossExtChangeRepository bossExtChangeRepository;
#Scheduled(fixedRate = 30000)
public void startService() {
System.out.println("Calling startService()");
BossExtChange bossExtChange = bossExtChangeRepository.findById(5256868L);
System.out.println("bossExtChange.getDescription()--->"+bossExtChange.getDescription());
System.out.println("Ending startService()");
}
}
Main Class :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication
#EnableScheduling
#PropertySource("classpath:application.properties")
public class EnverseDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnverseDemoApplication.class, args);
}
}
I have annotated the class as #Component and also method as #Scheduled(fixedRate = 30000) that will running as a scheduler. But while running the application as Spring Boot the scheduler does not trigger. The console show the below message:
2016-02-03 10:56:47.708 INFO 10136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-02-03 10:56:47.721 INFO 10136 --- [ main] com.mk.envers.EnverseDemoApplication : Started EnverseDemoApplication in 3.231 seconds (JVM running for 3.623)
2016-02-03 10:56:47.721 INFO 10136 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#49e202ad: startup date [Wed Feb 03 10:56:44 IST 2016]; root of context hierarchy
2016-02-03 10:56:47.721 INFO 10136 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2016-02-03 10:56:47.736 INFO 10136 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
Can anyone please help me out.
May be you can solve this problem by adding the #ComponentScan annotation in the configuration file
#SpringBootApplication
#EnableScheduling
#ComponentScan(basePackages = "com.mk.service")
#PropertySource("classpath:application.properties")
public class EnverseDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnverseDemoApplication.class, args);
}
}
I was able to solve the issue, I forgot to provide #service level annotation,
I have created the check List for #Scheduler, kindly go through every point one by one, It will help you to solve the issue.
Check for the #EnableScheduling on SpringBoot Main class.
Scheduled method should be annotated with #Scheduled, Follow the #Scheduled Method rules. a method should have the void return type, a method should not accept any parameters.
Make sure the class should be annotated with #Service or #Component Annotation so SpringBoot can make the object of that class.
The package of the scheduler jobs should be under the main Application class's package. e.g com.company is your main application class package then scheduler class package should be com.company.scheduler,
If you are using Cron expression confirm the same e.g #Scheduled( cron = "0 0/2 * * * ?"), This Cron expression will schedule the task for every 2 min.
Feel free to add more points in the comment so it will help to solve the issue.
It must be that you forgot to add #EnableScheduling annotation in your app class.
public static void main(String[] args) {
context = SpringApplication.run(YouApplication.class, args);
}
I was finally able to solve the above issue, I changed the package of my service class EnverseDemoService from package com.mk.service; to com.mk.envers.service;. This is because if the main configuration class EnverseDemoApplication is present in the package com.mk.envers. All the other classes in the boot application should be in the qualifying package. Eg: com.mk.envers.*;
In my case, was the lazy-initialization with value true which was preventing my #Component to be loaded by Spring at startup and #Scheduled method was never running.
Make sure the Spring Boot lazy initialization is disabled.
Please check if in application.properties you have
"spring.main.lazy-initialization=true"
Remove this from application.properties.
Even if you have all the configuration correct, this simple line will enable lazy loading due to which your #Component will initalize during application start.
As Swapnil already mentioned all the check-points to make sure while using cron. One additional thing you should do:
Always verify your cron expression whether it's in right format or not, using below reference site - http://www.freeformatter.com/cron-expression-generator-quartz.html#