camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: Camel Consumer in JAX-RS Web Service
Date Mon, 22 Jul 2013 12:59:13 GMT
You can look at using onCompletion or event notifier. Or even the NotifyBuilder.

You can find details on the Camel site using the search box on the front page.

On Sun, Jul 21, 2013 at 6:16 PM, traviskds <traviskds@gmail.com> wrote:
> I find Camel easy for difficult use cases but hard to simple use cases. Or
> maybe I haven't still mastered it.
>
> I have a JAX-RS web service that when executed, will dynamically create a
> route that reads mail from an IMAP server and then gets processed via a
> bean.
>
> My problem is that I need to add Thread.sleep(10000); so that the web
> service waits until the route completes.
>
> My issue is I want a better way to know if the route has completed instead
> of setting some arbitrary timer that has the potential for side effects.
> (e.g. waiting unnecessarily when the route might have already finished or
> even worse existing before the route has time to complete it's work.
>
> I am wondering how others might have done something similar to this
> requirement? Any ideas/suggestions are welcome as I have done everything
> possible including reading other posts in this forum, looking at the camel
> source code and test code without much success.
>
> My main code is below (note I have a JEE6 bootstrap class that starts
> CamelContext and which in turn is injected into the below jax-rs service)
>
> @Path("/Mail")
> public class ReadMail {
>
>         private static final Logger logger = LoggerFactory
>                         .getLogger(ReadMail.class);
>
>     @Inject
>     CdiCamelContext camelContext;
>
>         @Path("/read")
>         @GET
>         @Produces(MediaType.TEXT_HTML)
>         public Response getUser(@Context UriInfo uriInfo) {
>
>                 try {
>
>                         logger.debug(">>> Reading mail from mail server ..");
>                         MultivaluedMap<String, String> queryParameters = uriInfo
>                                         .getQueryParameters();
>                         String mailProtocol =
> queryParameters.getFirst("mailProtocol").toLowerCase();
>                         String mailHost = queryParameters.getFirst("host").toLowerCase();
>                         String userName = queryParameters.getFirst("username");
>                         String password = queryParameters.getFirst("password");
>
>                         final String MAIL_ENDPOINT_URI = mailProtocol + "://" + mailHost
>                                         + "?username="+ userName + "&password=" +
password + "&delete=false"
>                                         + "&debugMode=true&unseen=true&consumer.delay=5000";
>
>                         final MailBean mailBean = new MailBean();
>
>                         camelContext.addRoutes(new RouteBuilder() {
>                                 public void configure() throws Exception {
>                                         from(MAIL_ENDPOINT_URI).routeId("myRoute")
>                                         .bean(mailBean, "processMail");
>                                 }
>                         });
>
>                         Thread.sleep(10000);
>
>                         return
> Response.status(Response.Status.OK).entity(mailBean.getMessageBody().toString())
>                                         .build();
>
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>                 finally {
>                         try {
>                                 camelContext.stopRoute("myRoute");
>                                 camelContext.removeRoute("myRoute");
>                         } catch (Exception e) {
>                                 e.printStackTrace();
>                         }
>                 }
>
>                 return Response.status(Response.Status.NOT_FOUND)
>                                 .entity("Requested search did not return any results").build();
>
>         }
>
> }
>
> My Bean class is
>
> public class MailBean {
>
>         private StringBuilder messageBody = new StringBuilder();
>
>         public void processMail(Exchange exchange) throws MessagingException,
> IOException {
>
>                 System.out.println(">>>> Inside MailBean exchangeTest >>>>
> ");
>                 System.out.println("<<<< Message Id:"+
> exchange.getIn().getMessageId());
>
>         this.setMessageBody(this.getMessageBody().append("Message Id:"+
> exchange.getIn().getMessageId()+ "<br/><br/>"));
>
>         if (exchange.getIn().getBody() instanceof String) {
>
>                 this.setMessageBody(this.getMessageBody().append(((String)
> exchange.getIn().getBody()).replaceAll("(\r\n|\n)", "")+ "<br/><br/>"));
>
>         } else if (exchange.getIn().getBody() instanceof Multipart) {
>
>
> this.setMessageBody(this.getMessageBody().append((Utility.getMultipartText((Multipart)
> exchange.getIn().getBody())).replaceAll("(\r\n|\n)","")+ "<br/><br/>"));
>
>         }
>         }
>
>         public StringBuilder getMessageBody() {
>                 return messageBody;
>         }
>         public void setMessageBody(StringBuilder messageBody) {
>                 this.messageBody = messageBody;
>         }
>
> }
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-Consumer-in-JAX-RS-Web-Service-tp5735968.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Mime
View raw message