I'm trying to create a minimal web application using spring-boot which makes use of redis as a session store. The goal is to share session state between multiple instances of the web application. The application works fine without Redis (using "normal" sessions) but throws an exception after I enabled the configuration for Redis. I followed this tutorial. The application is started via "java -jar path_to.jar".
To make the app work again, it is sufficient to comment out the configuration class (see below).
The first attempt included the deployment to Heroku, but it's the same problem on my machine using a local Redis server. The environment variable for the connection string is set up properly.
The whole application consists only of the snippets shown below. Am I missing something? Thank you.
Configuration:
#Profile("production")
#Configuration
#EnableRedisHttpSession
public class ProductionRedisConfiguration {
#Bean
public JedisConnectionFactory connectionFactory() throws URISyntaxException {
JedisConnectionFactory redis = new JedisConnectionFactory();
String redisUrl = System.getenv("REDISCLOUD_URL");
URI redisUri = new URI(redisUrl);
redis.setHostName(redisUri.getHost());
redis.setPort(redisUri.getPort());
redis.setPassword(redisUri.getUserInfo().split(":",2)[1]);
return redis;
}
static class Initializer extends AbstractHttpSessionApplicationInitializer {
public Initializer() {
super(ProductionRedisConfiguration.class);
}
}
}
"App":
#EnableAutoConfiguration
#ComponentScan
public class Application {
private static final Logger LOGGER = getLogger(Application.class);
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
.
#Controller
public class GreetingController {
#RequestMapping("/")
public #ResponseBody String session(HttpServletRequest request) {
String name = (String) request.getSession().getAttribute("name");
if(name != null) {
return "Hello, " + name;
} else {
String newName = "User " + new Random().nextInt(1000000);
request.getSession().setAttribute("name", newName);
return "No session found, you are now called " + newName;
}
}
}
Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
Trace:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.c
ontext.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:140)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:609)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:806)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:795)
at com.backbase.progfun.Application.main(Application.java:17)
... 6 more
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:104)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:67)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServ
letContainerFactory.java:258)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletCon
tainerFactory.java:138)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:1
60)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
... 13 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Tomcat.start(Tomcat.java:341)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:77)
... 18 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 20 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 22 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 24 more
It appears that as of late you also need to set the REDIS_PROVIDER config var for apps deployed at Heroku.
Try running the following and for your app and lmk if it resolves the problem: heroku config:set REDIS_PROVIDER=REDISCLOUD_URL
Related
I'm trying to read datas from database into my spring-batch application.
I have one oracle table with a lot of fields and i just want to get a list of id (primary key).
So i have created a simple job with 2 steps (reader and writer).
#Configuration
#EnableBatchProcessing
public class BatchConfiguration {
#Bean
public Job importPurchase(
JobBuilderFactory jobBuilderFactory, Step findProSellerAccountStep, Step importPurchaseStep) {
return jobBuilderFactory
.get("importJob")
.incrementer(new RunIdIncrementer())
.start(findProSellerAccountStep)
// .next(importPurchaseStep)
.build();
}
#Bean
#JobScope
public Step findProSellerAccountStep(
StepBuilderFactory stepBuilderFactory,
ItemReader<Long> accountItemReader,
ItemWriter<Long> accountItemWriter) {
return stepBuilderFactory
.get("findProSellersStep")
.<Long, Long>chunk(10)
.reader(accountItemReader)
.writer(accountItemWriter)
.build();
}
}
And i have also created an item reader :
#Component
#JobScope
#Scope(proxyMode = ScopedProxyMode.DEFAULT)
public class AccountItemReader extends RepositoryItemReader<Long> {
public AccountItemReader(AccountEntityRepository accountRepository) {
// Get all elements
this.setMethodName("findAllAccount");
HashMap<String, Direction> sorts = new HashMap<>();
sorts.put("userAccountId", Sort.Direction.ASC);
this.setSort(sorts);
this.setPageSize(10);
this.setRepository(accountRepository);
}
}
Finally i have created a repository :
#Repository
public interface AccountEntityRepository extends PagingAndSortingRepository<AccountEntity, Long> {
#Query(
value =
"""
SELECT userAccountId
FROM AccountEntity
WHERE myparam = 30 and anotherparam = 20""",
countQuery =
"""
SELECT COUNT(userAccountId)
FROM AccountEntity
WHERE myparam = 30 and anotherparam = 20""")
Page<Long> findAllActiveAccount(Pageable page);
}
But when i try to execute my program with Intellij IDEA, i have got a big error :
2022-12-01 14:35:30.655 ERROR [o.s.b.SpringApplication ] - Application run failed
java.lang.IllegalStateException: Failed to execute ApplicationRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:765)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:752)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
at com.rakuten.merchant_index.MerchantIndexApplication.main(MerchantIndexApplication.java:10)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: Unable to deserialize the execution context
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao$ExecutionContextRowMapper.mapRow(JdbcExecutionContextDao.java:328)
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao$ExecutionContextRowMapper.mapRow(JdbcExecutionContextDao.java:312)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:61)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:723)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:651)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:744)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:757)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:815)
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao.getExecutionContext(JdbcExecutionContextDao.java:114)
at org.springframework.batch.core.explore.support.SimpleJobExplorer.getJobExecutionDependencies(SimpleJobExplorer.java:239)
at org.springframework.batch.core.explore.support.SimpleJobExplorer.getLastJobExecution(SimpleJobExplorer.java:104)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at jdk.proxy4/jdk.proxy4.$Proxy107.getLastJobExecution(Unknown Source)
at org.springframework.batch.core.JobParametersBuilder.getNextJobParameters(JobParametersBuilder.java:265)
at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.getNextJobParameters(JobLauncherApplicationRunner.java:213)
at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.execute(JobLauncherApplicationRunner.java:198)
at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.executeLocalJobs(JobLauncherApplicationRunner.java:173)
at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.launchJobFromProperties(JobLauncherApplicationRunner.java:160)
at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.run(JobLauncherApplicationRunner.java:155)
at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.run(JobLauncherApplicationRunner.java:150)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762)
... 10 common frames omitted
Caused by: com.fasterxml.jackson.databind.JsonMappingException: The class with [Ljava.lang.Object; and name of [Ljava.lang.Object; is not trusted. If you believe this class is safe to deserialize, you can add it to the base set of trusted classes at construction time or provide an explicit mapping using Jackson annotations or a custom ObjectMapper. If the serialization is only done by a trusted source, you can also enable default typing. (through reference chain: java.util.HashMap["accountIds"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:392)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:363)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:371)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
at com.fasterxml.jackson.databind.deser.std.StdDelegatingDeserializer.deserialize(StdDelegatingDeserializer.java:175)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:211)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:781)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:611)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:437)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:144)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:110)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserializeWithType(MapDeserializer.java:482)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:74)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3674)
at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.deserialize(Jackson2ExecutionContextStringSerializer.java:133)
at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.deserialize(Jackson2ExecutionContextStringSerializer.java:104)
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao$ExecutionContextRowMapper.mapRow(JdbcExecutionContextDao.java:325)
... 41 common frames omitted
Caused by: java.lang.IllegalArgumentException: The class with [Ljava.lang.Object; and name of [Ljava.lang.Object; is not trusted. If you believe this class is safe to deserialize, you can add it to the base set of trusted classes at construction time or provide an explicit mapping using Jackson annotations or a custom ObjectMapper. If the serialization is only done by a trusted source, you can also enable default typing.
at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer$TrustedTypeIdResolver.typeFromId(Jackson2ExecutionContextStringSerializer.java:353)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:159)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:97)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:211)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:781)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:357)
... 61 common frames omitted
I don't understand because there is no "HashMap["accountIds"]" into my actual java program.
So why jackson try to create this Map ? Is it an old cache ?
Finally, it was only an old code that was stored in the spring-batch context and that was reused each time the batch was restarted. I delete my job in database so i wouldn’t have to worry about it.
I'm trying to use the camel-bean-validator dependency in my project but I have a problem with group validation.
I'm using a springboot project with spring-boot-starter-parent 2.7.3 and these dependencies in pom.xml:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.18.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>3.18.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-validator-starter</artifactId>
<version>3.18.1</version>
</dependency>
My SimpleDto is:
#Data
public class SimpleDto {
#NotNull(groups = BasicValidation.class)
private Long id;
#NotBlank(groups = AdvancedValidation.class)
private String name;
#NotBlank
private String value;
}
And I created two interfaces for validation:
public interface AdvancedValidation {
}
public interface AdvancedValidation {
}
When I want to use a standard validation like this in my route the application starts without problem:
#Component
public class ValidationRoute extends RouteBuilder {
#Override
public void configure() throws Exception {
from("timer:val-timer?period=5000")
.process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
SimpleDto simpleDto = new SimpleDto();
exchange.getIn().setBody(simpleDto);
}
})
.log("Setted body: ${body}")
.to("bean-validator://x")
.log("Validated message");
}
}
but when I change the validation endpoint in:
.to("bean-validator://x?group=AdvancedValidation")
like in the example in Apache Camel documentation, here:
https://camel.netlify.app/components/latest/bean-validator-component.html
the application stop working and this is the error I have starting it:
org.apache.camel.FailedToStartRouteException: Failed to start route route1 because of null
at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:123)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doWarmUpRoutes(InternalRouteStartupManager.java:306)
at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:189)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartOrResumeRoutes(InternalRouteStartupManager.java:147)
at org.apache.camel.impl.engine.AbstractCamelContext.doStartCamel(AbstractCamelContext.java:3365)
at org.apache.camel.impl.engine.AbstractCamelContext.doStartContext(AbstractCamelContext.java:3033)
at org.apache.camel.impl.engine.AbstractCamelContext.doStart(AbstractCamelContext.java:2988)
at org.apache.camel.spring.boot.SpringBootCamelContext.doStart(SpringBootCamelContext.java:43)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
at org.apache.camel.impl.engine.AbstractCamelContext.start(AbstractCamelContext.java:2649)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:262)
at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:119)
at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:151)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:938)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at it.ale.CamelArchetypeTestApplication.main(CamelArchetypeTestApplication.java:16)
Caused by: org.apache.camel.RuntimeCamelException: java.lang.ClassNotFoundException: AdvancedValidation
at org.apache.camel.RuntimeCamelException.wrapRuntimeException(RuntimeCamelException.java:66)
at org.apache.camel.support.service.BaseService.doFail(BaseService.java:413)
at org.apache.camel.support.service.BaseService.fail(BaseService.java:342)
at org.apache.camel.support.service.BaseService.start(BaseService.java:132)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130)
at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1670)
at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:60)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130)
at org.apache.camel.impl.engine.DefaultChannel.doStart(DefaultChannel.java:126)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:116)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130)
at org.apache.camel.processor.Pipeline.doStart(Pipeline.java:224)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at org.apache.camel.support.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:89)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at org.apache.camel.impl.engine.RouteService.startChildServices(RouteService.java:396)
at org.apache.camel.impl.engine.RouteService.doWarmUp(RouteService.java:193)
at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:121)
... 24 common frames omitted
Caused by: java.lang.ClassNotFoundException: AdvancedValidation
at org.apache.camel.impl.engine.DefaultClassResolver.resolveMandatoryClass(DefaultClassResolver.java:103)
at org.apache.camel.component.bean.validator.BeanValidatorEndpoint.createProducer(BeanValidatorEndpoint.java:73)
at org.apache.camel.support.DefaultEndpoint.createAsyncProducer(DefaultEndpoint.java:200)
at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:242)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
... 44 common frames omitted
Process finished with exit code 1
I already tried to change the endpoint with:
.to("bean-validator://x?group=AdvancedValidation.class")
and this time the error was:
Caused by: org.apache.camel.RuntimeCamelException: java.lang.ClassNotFoundException: AdvancedValidation.class
Also adding the annotation #Component to the interface I haven't solved the problem.
Does anyone know why it is not working and how to solve it?
I followed the documentation and there it seem work.
Using the same interfaces and the same project in a RestController and validating it with #Valid or #Validated(AdvancedValidation.class) worked correctly, so this is a Camel issue
I probably solved it by using all the class path related to group:
.to("bean-validator://x?group=com.application.dto.validation.group.AdvancedValidation")
I have a class that configures Cassandra database:
#Configuration
#EnableCassandraRepositories(basePackages = { BASE_PACKAGE })
public class CassandraConfig {
#Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
#Value("${spring.data.cassandra.keyspace-name}")
private String keyspaceName;
#Value("${spring.data.cassandra.datacenter-name}")
private String datacenterName;
#Value("${spring.data.cassandra.port}")
private int port;
#Bean
public CqlSessionFactoryBean session() {
CqlSessionFactoryBean session = new CqlSessionFactoryBean();
session.setContactPoints(contactPoints);
session.setKeyspaceName(keyspaceName);
session.setLocalDatacenter(datacenterName);
session.setPort(port);
return session;
}
...
}
And I have an empty test that just loads spring context
#SpringBootTest
class ApplicationTest {
#Test
void contextLoads() {
}
}
When I run it, I see this exception message:
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=127.0.0.1/<unresolved>:9042, hostId=null, hashCode=29caef8c): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
at app//com.datastax.oss.driver.api.core.AllNodesFailedException.copy(AllNodesFailedException.java:141)
at app//com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures.getUninterruptibly(CompletableFutures.java:149)
at app//com.datastax.oss.driver.api.core.session.SessionBuilder.build(SessionBuilder.java:835)
at app//org.springframework.data.cassandra.config.CqlSessionFactoryBean.buildSystemSession(CqlSessionFactoryBean.java:498)
at app//org.springframework.data.cassandra.config.CqlSessionFactoryBean.afterPropertiesSet(CqlSessionFactoryBean.java:451)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
... 166 more
Suppressed: com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)
at app//com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.fail(ProtocolInitHandler.java:356)
at app//com.datastax.oss.driver.internal.core.channel.ChannelHandlerRequest.writeListener(ChannelHandlerRequest.java:87)
at app//io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)
at app//io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:552)
at app//io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)
at app//io.netty.util.concurrent.DefaultPromise.addListener(DefaultPromise.java:184)
at app//io.netty.channel.DefaultChannelPromise.addListener(DefaultChannelPromise.java:95)
at app//io.netty.channel.DefaultChannelPromise.addListener(DefaultChannelPromise.java:30)
at app//com.datastax.oss.driver.internal.core.channel.ChannelHandlerRequest.send(ChannelHandlerRequest.java:76)
at app//com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.send(ProtocolInitHandler.java:193)
at app//com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler.onRealConnect(ProtocolInitHandler.java:124)
at app//com.datastax.oss.driver.internal.core.channel.ConnectInitHandler.lambda$connect$0(ConnectInitHandler.java:57)
at app//io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)
at app//io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571)
at app//io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550)
at app//io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)
at app//io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)
at app//io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:609)
at app//io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:117)
at app//io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321)
at app//io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337)
at app//io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
at app//io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at app//io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at app//io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at app//io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at app//io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at app//io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base#17.0.1/java.lang.Thread.run(Thread.java:833)
Suppressed: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /127.0.0.1:9042
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: io.netty.channel.StacklessClosedChannelException
at io.netty.channel.AbstractChannel$AbstractUnsafe.flush0()(Unknown Source)
As I understand, it tries to connect to the database but it cannot, thus, it fails.
I tried different ways to exclude this class from context loading like #EnableAutoConfiguration(exclude = CassandraConfig.class) but then it fails with
java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes: com.myproject.test.config.CassandraConfig
Is there some advices or best practices how to solve this problem?
The error you're getting makes sense, CassandraConfig is not an autoconfiguration but a configuration class.
To exclude it you could use a Spring profile:
#Configuration
#Profile("!test")
public class CassandraConfig {
Then in your test class:
#SpringBootTest(properties = "spring.profiles.active=test")
class ApplicationTest {
I am trying to run a spring boot project which runs perfectly on embedded tomcat but when deployed to wildfly 21.x throws java.lang.NoClassDefFoundError: org/apache/catalina/connector/Connector
Any help would be appreciated.
Below is my code snippet.
Main file:-
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
public class CdPlaylistadapterApplication {
#Value("${http.port}")
private int httpPort;
public static void main(String[] args) {
SpringApplication.run(CdPlaylistadapterApplication.class, args);
}
#Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(httpPort);
return connector;
}
}
My pom.xml snippet
<cxf.version>3.4.0</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Error:-
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.att.dtss.playlistadapter.CdPlaylistadapterApplication] from ClassLoader [ModuleClassLoader for Module "deployment.cd-playlistadapter-0.0.1-SNAPSHOT.war" from Service Module Loader]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:455)
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:151)
... 39 more
Caused by: java.lang.NoClassDefFoundError: org/apache/catalina/connector/Connector
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
... 41 more
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.connector.Connector from [Module "deployment.cd-playlistadapter-0.0.1-SNAPSHOT.war" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:255)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
... 45 more
The class org.apache.catalina.connector.Connector is part of Tomcat. In order for your application to work correctly on external servlet containers, the embedded copy of Tomcat must not be on the classpath of the web application (i.e. in /WEB-INF/lib). You set it correctly by setting the scope of the spring-boot-starter-tomcat to provided.
However now you have a problem, because the CdPlaylistadapterApplication has Connector in one of its method signatures and Spring Boot fails while creating an instance of the CdPlaylistadapterApplication class.
To solve it you need to move Tomcat specific configuration to another class (even nested) and protect it with the #ConditionalOnClass annotation:
#SpringBootApplication
public class CdPlaylistadapterApplication extends SpringBootServletInitializer {
#Configuration
#ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
static class EmbeddedConfiguration {
#Value("${http.port}")
private int httpPort;
#Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(httpPort);
return connector;
}
}
public static void main(String[] args) {
SpringApplication.run(CdPlaylistadapterApplication.class, args);
}
}
I have implemented the SevletExtention mechanism of undertow api. The code is like
public class MyServletExtentionClass implements ServletExtension{
#Override
public void handleDeployment(final DeploymentInfo deploymentInfo, final ServletContextImpl servletContext) {
deploymentInfo.addInitialHandlerChainWrapper(new HandlerWrapper() {
#Override
public HttpHandler wrap(HttpHandler handler) {
return Handlers.path()
.addPrefixPath("/", handler)
.addPrefixPath("/hello", new MyHandler());
}
});
}
}
also i had added the file io.undertow.servlet.ServletExtension under META_INF/services with the content com.handler.MyServletExtentionClass
But when i deploy the war file in wildfly I am getting an exception like below.
jboss.undertow.deployment.default-server.default-host./webapps: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./webapps: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_71]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_71]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_71]
Caused by: java.lang.AbstractMethodError
at io.undertow.servlet.core.DeploymentManagerImpl.handleExtensions(DeploymentManagerImpl.java:244)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:149)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
... 3 more
try this
...
import io.undertow.servlet.ServletExtension;
import javax.enterprise.inject.spi.Extension;
public class MyServletExtentionClass implements Extension, ServletExtension {
....