Camel 动态路由以及HttpServletRequest/Reponse的使用

阅读更多

本实验的配置文件 camel-context.xml 内容:

 



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

   
   
   
   
   
       
       
              
             
               
                

       

      
       
            
          
          
          
            
          
          
          
          
       
  
       
   

//TProcessor.java 的源代码,


import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayInputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;;

public class TProcessor implements Processor {

    private static final Log log = LogFactory.getLog(TProcessor.class);
 

    public void process(Exchange exchange) throws Exception {
        log.info("-->process exchange:"+exchange);
        log.info("--ESBProcessor---in--");
       
        HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
       
        Message in=exchange.getIn();
       
        System.out.println("process-->"+exchange);
        System.out.println("head="+in.getHeaders()+
                ",\r\n\tip="+req.getRemoteAddr()+",\r\n\turi="+req.getRequestURI());
        String sb="none input!";
        ByteArrayInputStream bin=new ByteArrayInputStream(sb.getBytes());
        exchange.getIn().setBody(bin);
       
    }
   
    public String parseRoute(Exchange exchange){
       
        Message in=exchange.getIn();
        Map hs=in.getHeaders();
        if(hs==null){
            System.out.println("-----end--3333---");
            return null;
        }
        if(hs.containsKey("_finished")){ //达到退出循环的条件
            System.out.println("-----end--2222---");
            return null;
        }
       
        HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
        if(req==null){
            System.out.println("-----end--1111---");
            return null;
        }
       
        System.out.println("\r\n--------------------------\r\n");
        System.out.println("parseRoute-->"+exchange);
        System.out.println("parseRoute-->head="+hs);
         
        System.out.println("\r\n\t----from ip="+req.getRemoteAddr()+",uri="+req.getRequestURI());
        String uri=req.getRequestURI();
        uri=uri.replace('/', '-');
        if(uri.startsWith("-")){
            uri=uri.substring(1);
        }
       
        uri="direct:"+uri; 
       
        System.out.println("parseRoute-->to:{"+uri+"}"); 
       
        return uri;
    }
   
    public void end(Exchange exchange){
        Message in=exchange.getOut();
        Map hs=in.getHeaders();
        hs.put("_finished", true);  //动态路由会循环路由,设置退出循环的条件
    }
   
}

 

Time.java 源代码


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Time {
    private static final Log log = LogFactory.getLog(Time.class);
   
    public Time(){
        super();
    }
   
    public String showBody(Exchange exchange) throws Exception {  
        System.out.println("\r\n\r\n");
       
        System.out.println("showBody-->From context:"+ exchange.getContext().getName());
       
        Message in=exchange.getIn();
        Object os=in.getBody();
        String msg=String.valueOf(os)+"[String]";
        if(os instanceof ByteArrayInputStream){
            byte []bs=read((ByteArrayInputStream)os); 
            msg=new String(bs,"utf-8")+"[stream]";
        }
        System.out.println("showBody-->msg="+msg);
       
        return msg;
    }
   
    private byte[] read(ByteArrayInputStream in){
        int len=in.available();
        byte []data=new byte[len];
        in.read(data,0,data.length);
        return data;
    }
   
    public void showHeads(Map h){
        System.out.println("\r\n\r\n");
        System.out.println("showHeads--> heads="+h);
    }
   
    public Request parseRequest(Exchange exchange,String body) {
       
        System.out.println("\r\n\r\n");
        Message in=exchange.getIn();
        Map ms=in.getHeaders();
        Request r=new Request();
        r.setHead(ms);
        System.out.println("parseRequest--> heads="+ms+"\r\n\t-->body="+body); 
       
        r.setBody(body);
        return r;
    }
   
    public  String process(Request req) {
        System.out.println("\r\n\r\n");
        System.out.println("process--> heads="+req.getHead()+"\r\n\t-->body="+req.getBody());
        String s="你好,系统时间为:"+new Date();
        return s;
    }
   
    public  void response(Exchange exchange,String html) throws IOException {
        System.out.println("\r\n\r\n");
       
        /*
        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html" + "; charset=utf-8");
        //out.getOut().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream" + "; charset=utf-8"); 
        exchange.getOut().setBody(html); 
        */
        Message in=exchange.getIn();
        Map hs=in.getHeaders();
        System.out.println("response 1-->head="+hs);
        System.out.println("response 1-->html="+html); 
       
        //HttpServletResponse res = exchange.getOut().getBody(HttpServletResponse.class);
        //HttpServletResponse res = (HttpServletResponse)hs.get(Exchange.HTTP_SERVLET_RESPONSE);
        HttpServletResponse res = exchange.getIn().getBody(HttpServletResponse.class);
       
        //HttpServletResponse.class);
       
        res.addHeader(Exchange.CONTENT_TYPE, "text/html; charset=utf-8");
        PrintWriter writer = res.getWriter();
        writer.print(html);
    }
   
    public  void response(Exchange exchange) throws IOException {
        System.out.println("\r\n\r\n");
        String html="欢迎你!";
       
        Message in=exchange.getIn();
        Map hs=in.getHeaders();
       
        System.out.println("response 2-->head="+hs);
        System.out.println("response 2-->html="+html);
       
        /*
        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html" + "; charset=utf-8");
        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream" + "; charset=utf-8"); 
        exchange.getOut().setBody(html); 
        */
     
        HttpServletResponse res = exchange.getIn().getBody(HttpServletResponse.class);

        //HttpServletResponse / HttpServletRequest 都 从 In 初获得,


        res.addHeader(Exchange.CONTENT_TYPE, "text/html; charset=utf-8");
        PrintWriter writer = res.getWriter();
        writer.print(html);
         
    }
   
   
    public  String toHtml(Object r) {
        System.out.println("\r\n\r\n");
        System.out.println("toHtml--> object="+r); 
        return String.valueOf(r);
    }


}

 

 

你可能感兴趣的:(Camel 动态路由以及HttpServletRequest/Reponse的使用)