[
https://issues.apache.org/jira/browse/CAMEL-5312?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13288827#comment-13288827
]
Amit Patel commented on CAMEL-5312:
-----------------------------------
I wrote a Grails application. I don't know how to send this application to you.
In this application, I can add route using Controller. I found issue in
org.restlet.ext.spring.SpringServerServlet it only call createServer method on
it first request. so
I extend the SpringServerServlet and call createServer method when new route is
added. Please find this Servlet in attachment.
Improvement: If component store isNewRoute added variable and ServerServlet
reset this variable i don't need isNewRouteAdded method, so i can gain
performance.
import org.apache.camel.CamelContext
import org.apache.camel.ProducerTemplate;
import grails.spring.BeanBuilder
import org.apache.camel.spring.spi.ApplicationContextRegistry
import org.apache.camel.model.RoutesDefinition
import java.io.ByteArrayInputStream
import javax.xml.bind.Unmarshaller
import org.apache.camel.model.RouteDefinition
import javax.xml.bind.JAXBContext
import javax.xml.bind.JAXBException
import javax.xml.bind.Unmarshaller
class BootService {
def restletComponent
def restletComponentService
static transactional = false;
static expose = ['jmx']
def appCtx;
CamelContext ctx;
def grailsApplication;
// Camel template - a handy class for kicking off exchanges
ProducerTemplate template
def serviceMethod() {
}
def init() {
start();
}
def start() {
println "inside start"
appCtx
=org.codehaus.groovy.grails.commons.ApplicationHolder.getApplication().getMainContext()
//Restlet URI matching set default to EQUALS
//
restletComponent.defaultHost.setDefaultMatchingMode(org.restlet.routing.Template.MODE_EQUALS);
// Load user defined beans into context
// Load jars from NIMBUS_HOME/lib
// Load beans from NIMBUS_HOME/conf/*Beans.groovy
def bb = new BeanBuilder(appCtx)
def bbAppCtx = bb.createApplicationContext()
//Create a ApplicationContexRegistry from ApplicationContext
//def applicationContextRegistry = new
ApplicationContextRegistry(bbAppCtx)
//create camel-1 conext to be used throughout this application
//ctx = new
org.apache.camel.impl.DefaultCamelContext(applicationContextRegistry)
//create camel-1 conext to be used throughout this application
ctx = new org.apache.camel.spring.SpringCamelContext(bbAppCtx)
// Camel template - a handy class for kicking off exchanges
template = ctx.createProducerTemplate();
ctx.start()
//loadRoutes();
}
def addRoute(routeId,routeDef) {
try{
JAXBContext context =
JAXBContext.newInstance("org.apache.camel:org.apache.camel.model:org.apache.camel.model.config:org.apache.camel.model.dataformat:org.apache.camel.model.language:org.apache.camel.model.loadbalancer");
Unmarshaller unmarshaller =
context.createUnmarshaller();
Object value = unmarshaller.unmarshal(new
StringReader(routeDef));// RoutesDefinition routes =
camelContext.loadRoutesDefinition(is);
RouteDefinition route = (RouteDefinition)value;
// def routeName =
cr.namespace+"."+cr.routeName+"."+cr.routeVersion
route.routeId(routeId)
// // add or update route in running camel context
ctx.stopRoute(routeId)
ctx.removeRoute(routeId)
//if(cr.deployStatus=="Active")
ctx.addRouteDefinitions(Collections.singletonList(route));
}catch(all)
{
all.printStackTrace()
}
try{
def status = ctx.getRouteStatus(routeId)
def msg = "${routeId} status: ${status}"
println "\t\t${msg}"
}catch(all){
all.printStackTrace()
}
}
}
/* DO NOT USE THIS CONTROLLER. THIS CONTROLLER IS WRITTEN FOR LOAD TEST THE ADD
AND DELETE ROUTE, AND SHOULD BE DELETED AFTER LOAD TEST IS OVER.
*
*
*/
class AddRouteController {
def bootService
def add = {
def routeDef =params.routeDefinition
def routeId =params.routeId
try {
bootService.addRoute(routeId,routeDef)
render "Route Added"
}catch(all) {
all.printStackTrace()
}
}
}
> Adding Restlet routes with relative paths to Apache Camel context does NOT
> work after I send first request to restlet route
> ----------------------------------------------------------------------------------------------------------------------------
>
> Key: CAMEL-5312
> URL: https://issues.apache.org/jira/browse/CAMEL-5312
> Project: Camel
> Issue Type: Bug
> Components: camel-restlet
> Affects Versions: 2.9.1
> Environment: Grails, Apache Camel
> Reporter: Amit Patel
> Attachments: RestletSpringServlet.java, RestletSpringServlet.java
>
>
> Adding Restlet routes with relative paths to Apache Camel context does NOT
> work after I send first request to restlet route.
> 1) Add Restlet route restletRoute1 with relative path to Apache Camel context
> 2) Send a request to restletRoute1 (works)
> 3) Add a route restletRoute2 with relative path to Apache Camel context
> 4) Send a request to restletRoute2 (does not works). RestletServlet
> does not find the restlet endpoint and return Not Found message.
> when I checked jconsole It saw the restletRoute1 and restletRoute2 started
> successfully. If I add restletRoute1 and restletRoute2 then send a
> request to restletRoute1 and restletRoute2 both works. Our
> application supports add route on-the-fly feature, so we don't have
> to start the application every time when we add a new route. Because of
> the above issue we can't integrate restlet to our application.
> Routes
> -------
> <routes xmlns="http://camel.apache.org/schema/spring">
> <route>
> <from uri="restlet:/restletRoute1"/>
> <setHeader headerName="Content-Type">
> <constant>text/plain</constant>
> </setHeader>
> <setBody>
> <constant>restletRoute1</constant>
> </setBody>
> </route>
> <route>
> <routes xmlns="http://camel.apache.org/schema/spring">
> <route>
> <from uri="restlet:/restletRoute2"/>
> <setHeader headerName="Content-Type">
> <constant>text/plain</constant>
> </setHeader>
> <setBody>
> <constant>restletRoute2</constant>
> </setBody>
> </route>
> <route>
> libs
> -----
> 'org.apache.camel:camel-restlet:2.9.1'
> 'org.restlet.jee:org.restlet.ext.spring:2.0.13'
> Web.xml
> -----------
> <servlet>
> <servlet-name>RestletServlet</servlet-name>
>
> <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
> <init-param>
> <param-name>org.restlet.component</param-name>
> <param-value>restletComponent</param-value>
> </init-param>
> </servlet>
> <servlet-mapping>
> <servlet-name>RestletServlet</servlet-name>
> <url-pattern>/rs/*</url-pattern>
> </servlet-mapping>
> Resources.groovy
> -----------------
> restletComponent(org.restlet.Component)
> restletComponentService(org.apache.camel.component.restlet.RestletComponent,
> ref("restletComponent"))
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira