camel 3.8 generates REST routes twice - rest

I faced a problem with camel REST routes when I switched from camel version 3.5.0 to version 3.8.0.
In the version 3.8.0 all routes from the REST service are generated twice! So at start of the camel context the exception is thrown:
Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
In detail:
I have a simple RouteBuilder to generate REST routes:
#Component
public class RestInterface extends RouteBuilder {
#Override
public void configure() throws Exception {
restConfiguration()
.component("jetty")
.host(urlInfo.getHost())
.port(urlInfo.getPort())
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true")
.clientRequestValidation(true);
rest("/")
.get("/start").outType(RepositoryPackageInfo.class).to("direct:start")
.get("/stop").outType(RepositoryPackageInfo.class).to("direct:stop");
// ...
}
}
When I start this code with camel 3.8.0 I get following console output:
...
jetty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 11.0.4+11
Started o.e.j.s.ServletContextHandler#5f65e0c0{/,null,AVAILABLE}
Started ServerConnector#27b7204{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Started #4095ms
Error starting CamelContext (camel-1) due to exception thrown: Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
org.apache.camel.FailedToStartRouteException: Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
...
Apache Camel 3.8.0 (camel-1) shutting down (timeout:45s)
Stopped ServerConnector#27b7204{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Stopped o.e.j.s.ServletContextHandler#5f65e0c0{/,null,UNAVAILABLE}
Routes shutdown summary (total:40 stopped:40) <-------------- total 40 routes!!!
...
Stopped route8 (rest://get:/:/stop)
Stopped route7 (rest://get:/:/start)
Stopped route2 (rest://get:/:/stop)
Stopped route1 (rest://get:/:/start) <-------------- the routes "stop" and "start" are doubled!!!
...
If I run the same code with camel 3.5.0, the application starts without exceptions, and camel has only 38 routes:
etty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 11.0.4+11
Started o.e.j.s.ServletContextHandler#6cbb6c41{/,null,AVAILABLE}
Started ServerConnector#42172065{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Started #4034ms
Route: route1 started and consuming from: jetty:http://0.0.0.0:6060/start
Route: route2 started and consuming from: jetty:http://0.0.0.0:6060/stop
...
Total 38 routes, of which 38 are started <-------------- total 38routes!!!
...
Completed initialization in 16 ms
Thank you in advance. I hope somebody can help me with this issue.
For completeness, here is a part of my pom.xml
<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rest</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-zipfile</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-zipfile-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>

Check if configure method on your RestInterface RouteBuilder is getting called more than once. Multiple calls cause duplicate RouteDefinitions to be added to the RouteBuilder instance which can lead to this error.
One way to solve this is to use boolean value in your RouteBuilder to check if configure method has already been called. Then use this value to early out or to throw an exception.
boolean isConfigured = false;
#Override
public void configure() throws Exception {
if(isConfigured){
return;
}
// Configure routes
isConfigured = true;
}
Encountered similar problem with Camel 2.x running in Karaf + OSGi where modifying config file lead to same error.
Basically configuring CamelContext service caused it to restart while the RouteBuilder services it referenced kept their state leading to situation where their configure method got called the second time leading to same error.

Related

The background display of the startup project is as follows

Spring integrates jwt to realize login.
pom.xml configuration
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
application.yml configuration
logging:
level:
com.xxxx.server.mapper: debug
jwt:
tokenHeader: Authorization
secret: yeb-secret
expiration: 604800
tokenHead: Bearer
Running the project background prompts the following information.
Description:
Failed to bind properties under 'logging.level.jwt.tokenheader' to org.springframework.boot.logging.LogLevel:
Property: logging.level.jwt.tokenheader
Value: "Authorization"
Origin: class path resource [config/application.yml] - 52:20
Reason: failed to convert java.lang.String to org.springframework.boot.logging.LogLevel (caused by java.lang.IllegalArgumentException: No enum constant org.springframework.boot.logging.LogLevel.Authorization)
Why it can't works?
thanks!!

Spring Cloud Stream unable to detect message router

I'm trying to set up a simple cloud stream Sink but keep running into the following errors.
I've tried several binders and they all keep giving the same error.
"SEVERE","logNameSource":"org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter","message":"
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method binderAwareRouterBeanPostProcessor in org.springframework.cloud.stream.config.BindingServiceConfiguration required a bean of type '[ Lorg.springframework.integration.router.AbstractMappingMessageRouter;' that could not be found.
Action:
Consider defining a bean of type '[ Lorg.springframework.integration.router.AbstractMappingMessageRouter;' in your configuration.
I'm trying to use a simple Sink to log an incoming message from a kafka topic
#EnableBinding(Sink.class)
public class ReadEMPMesage {
private static Logger logger =
LoggerFactory.getLogger(ReadEMPMesage.class);
public ReadEMPMesage() {
System.out.println("In constructor");
}
#StreamListener(Sink.INPUT)
public void loggerSink(String ccpEvent) {
logger.info("Received" + ccpEvent);
}
}
and my configuration is as follows
# Test consumer properties
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.group-id=testEmbeddedKafkaApplication
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.ByteArrayDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.ByteArrayDeserializer
# Binding properties
spring.cloud.stream.bindings.output.destination=testEmbeddedOut
spring.cloud.stream.bindings.input.destination=testEmbeddedIn
spring.cloud.stream.bindings.output.producer.headerMode=raw
spring.cloud.stream.bindings.input.consumer.headerMode=raw
spring.cloud.stream.bindings.input.group=embeddedKafkaApplication
and my pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
TL;DR - check your version of Spring Boot and try upgrading it a few minor revs.
I ran into this problem on a project after upgrading from Spring Cloud DALSTON.RELEASE to Spring Cloud Edgware.SR4 -- it was strange because other projects worked fine but there was a single one that didn't.
After further investigation I realized that the troublemaker project was using Spring Boot 1.5.3.RELEASE and others were using 1.5.9.RELEASE
After upgrading Spring Boot to 1.5.9.RELEASE things seemed to start working

Exception in thread "main" java.io.IOException: Cannot initialize Cluster

I am trying to run a simple Hadoop Map reduce program on eclipse in windows. Iam getting the following exception.
Exception in thread "main" java.io.IOException: Cannot initialize Cluster. Please check your configuration for mapreduce.framework.name and the correspond server addresses.
at org.apache.hadoop.mapreduce.Cluster.initialize(Cluster.java:121)
at org.apache.hadoop.mapreduce.Cluster.<init>(Cluster.java:83)
at org.apache.hadoop.mapreduce.Cluster.<init>(Cluster.java:76)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1188)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1184)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Unknown Source)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1548)
at org.apache.hadoop.mapreduce.Job.connect(Job.java:1183)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1212)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1236)
at com.hadoop.mapreduce.WordCountDriverClass.main(WordCountDriverClass.java:41)
These are the jar files I have added in the project.
com.google.guava_1.6.0.jar
commons-configuration-1.7.jar
commons-lang-2.6.jar
commons-logging-1.1.3.jar
commons.collections-3.2.1.jar
guava-13.0.1.jar
hadoop-annotations-2.7.2.jar
hadoop-auth-2.6.0.jar
hadoop-common-2.3.0.jar
hadoop-common.jar
hadoop-mapreduce-client-core-2.0.2-alpha.jar
hadoop-mapreduce-client-core-2.7.2.jar
hadoop-mapreduce-client-jobclient-2.2.0.jar
hadoop-test-1.2.1.jar
log4j-1.2.17.jar
slf4j-api-1.7.7.jar
slf4j-simple-1.6.1.jar
I have added those jar files after checking the exception messages in the console. But I couldn't understand this exception.
Can anyone please help me fixing this.
This is my driver class.
Configuration conf = new Configuration();
// Creating a job
Job job = Job.getInstance(conf,"WordCountDriverClass");
job.setJarByClass(WordCountDriverClass.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setNumReduceTasks(2);
job.setInputFormatClass(KeyValueTextInputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("inputfiles"));
FileOutputFormat.setOutputPath(job, new Path("outputfiles"));
job.waitForCompletion(true);
Looks like you are running the wordcount example, what it requires are 1.2.1 hadoop-core and 2.2.0 hadoop-common. If you use Maven instead, the config should be as simple as
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.2.0</version>
</dependency>

Need help in REST ASSURED

I am starting with REST Assured, getting error while executing below code :
Code 1-
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method equalTo(String) is undefined for the type
Code 2 -
RestAssured.expect().statusCode(200).
body(
"name", Matchers.equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
Below are the only 2 methods in my class, I am having issue with first one, second one is running fine. Please let me know what I am missing in first method.
Method -1
public static void testCountriesCallingCode() {
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7").asString());
}
Method-2
public static void testCountriesCallingCodeUsingJSONPATH(){
Response res = RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(res.getStatusCode());
String json = res.asString();
JsonPath jp = new JsonPath(json);
System.out.println(jp.get("name"));
}
Thanks Hti, your answer worked. Without the other dependencies, Rest Assured kind of works. I have no idea why Rest Assured website does not note this. Following in pom.xml worked
<properties>
<rest-assured.version>3.0.2</rest-assured.version>
<resteasy.version>3.0.17.Final</resteasy.version>
</properties>
...
<!-- Jackson is for allowing you to convert pojo (plain old Java object) into JSON -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>2.4.11</version>
<scope>test</scope>
</dependency>
Change the body of your first example to:
body(
"[0].name", equalTo("Russia")
)
That is because the JSON response from the server is not an object, but an array, and you have to query for the first object ([0]), then the name (.name).
For the Code-1, for equalTo() method you have to import org.hamcrest.Matchers.*;
For the exception in code 2, it is very hard to mention without looking at the RESPONSE but try to follow below link if you have nested generic parameters in your response.
How to validate nested response using REST Assured?
Please let me know if you have any issue or question. Thanks!
Even though this question is old, I just stumpled upon the second problem:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
This is due to missing dependencies. In my case I needed to add the dependencies for xml-path and groovy-xml, even though I'm just working with JSON data. So the best thing to do is resolving the dependencies transitively.
equalTo comes from Hamcrest which is a JUnit dependency contained within the JUnit jar. You probably just need to import the static method for it from Hamcrest.
import static org.hamcrest.core.IsEqual.*;
Add a static package for equal to:
import static org.hamcrest.Matchers.*;

JAX-RS Jersey Error java.lang.NoSuchMethodError

I wanted to implement a file-upload function for my jersey based rest server.
when i set my pom.xml (using maven) to:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.0</version>
</dependency>
i get following error:
JAX-RS Jersey Error java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.ReflectionHelper.getContextClassLoaderPA()Ljava/security/PrivilegedAction;
without the "jersey-media-multipart"-dependency the rest server is working but i cant use the file-upload functions.
Following the important part of source code:
ResourceConfig resourceConfig = new ResourceConfig(RestWebServer.class);
//resourceConfig.register(MultiPartFeature.class);
URI endPoint = new URI(pathServer);
server = JdkHttpServerFactory.createHttpServer( endPoint, resourceConfig );
RestWebserver.java:
#Path("/fileupload")
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormParam("file") InputStream uploadedInputStream,
#FormParam("file") FormDataContentDisposition fileDetail)
{
String uploadedFileLocation = "c://" + fileDetail.getFileName();
// save it
saveToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
Not a Jersey user so I am just guessing, but you probably have a jar mismatch.
Try replacing your second entry with this:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.4.1</version>
</dependency>
Basing my guess on the chapter 8, have you registered your client/server code?