Announcement Announcement Module
Collapse
No announcement yet.
How to poll a url? http outbound nor feed inbound seem to fit? :( Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to poll a url? http outbound nor feed inbound seem to fit? :(

    Hi all,

    I'm having a hard time figuring out how we should poll a url. (I'm creating some kind of webserver-polling print client)

    This seems like something inbound, but the only thing I can I can find is an outbound-channel-adapter using an url. And we can put a poller inside it.

    The feed inbound channel adapter on the other hand doesn't support uri variables or response types.

    Seems like nothing suits my needs, or am I overlooking something here?

    Thanks a lot in advance,

    Jochen

  • #2
    Well, when it comest to HTTP polling there is really no such thing since you actually sending HTTP Request in order to receive HTTP Response, so in your case HTTP Outbound Gateway would be the ideal. To emulate polling (or sending parameterized HTTP request) based on some interval or other trigger all you need is basic Inbound Channel Adapter (with poller) before the HTTP Outbound Gateway

    Inbound Channel Adapter (poller) -> HTTP Outbound Gateway -> Whatever else.

    So the configuration would look similar to this

    Code:
    <int:inbound-channel-adapter channel="httpGatewayChannel". . .>
         <bean class="foo.bar.MyMessageSource"/>
         <poller fixed-rate="1000" />
    </int:inbound-channel-adapter>
    
    <int:channel id="httpGatewayChannel"/>
    
    <int-http:outbound-gateway request-channel="httpGatewayChannel". . . .>
         <int-http:uri-variable expression="payload"/>
    </int-http:outbound-gateway>
    So, in the above foo.bar.MyMessageSource is what's going to produce the Message that will be sent to Http Outbound Gateway. It could implement MessageSource or just be a POJO as long as it returns a non-void method (the return value will become a Message payload). MyMessageSource's method will be invoked every second and produce a Message which will be sent to HTTP Outbound Gateway where you can see that you can map URI variables to any part of the Message.

    I am pretty sure that is what you are looking for. Let us know.

    Comment


    • #3
      Also, if you don't even need the POJO, you can use an 'expression' attribute on the inbound-channel-adapter. Then, use SpEL to define the payload, e.g.:
      Code:
      <int:inbound-channel-adapter channel="httpGatewayChannel" expression="'whatever'" ...>
           <poller fixed-rate="1000" />
      </int:inbound-channel-adapter>

      Comment


      • #4
        Thanks heaps for the super fast reply guys!

        I manged to create the polling GET requests returning the json from the server like this:
        http://www.appletree.be/temp/2011-05-04_131931.png

        And find it pretty amazing how spring integration + the visualizer managed to streamline the whole flow. Awesome work!

        The xml config:
        PHP Code:
            <int:inbound-channel-adapter channel="httpGatewayChannel"
                
        method="getId" auto-startup="true">
                <
        int:poller fixed-rate="10000" />
                <
        bean class="be.tradelec.print.client.PrintClientUtil" />
            </
        int:inbound-channel-adapter>

            <
        int:channel id="httpGatewayChannel" />

            <
        int-http:outbound-gateway
                url
        ="http://localhost:8080/TradPrintServer/printqueue/{id}"
                
        expected-response-type="java.lang.String" http-method="GET"
                
        auto-startup="true" request-channel="httpGatewayChannel"
                
        reply-channel="replyChannel">
                <
        int-http:uri-variable name="id" expression="payload" />
            </
        int-http:outbound-gateway>

            <
        int:channel id="replyChannel" />

            <
        int:logging-channel-adapter level="TRACE" auto-startup="true"
                
        channel="replyChannel" id="replyLogger" />

            <
        int:json-to-object-transformer
                output
        -channel="channel2"
                
        type="be.tradelec.model.PrintQueue" input-channel="replyChannel"/>

            <
        int:channel id="channel2"></int:channel>
            <
        int:logging-channel-adapter id="lastLogger" channel="channel2"
                
        level="TRACE" log-full-message="true" auto-startup="true">
            </
        int:logging-channel-adapter
        But now I'm having troubles reconstructing my domain object (PrintClientUtil) from the response. I don't get any errors, but my loggers don't seem to output something that seems to be the objects I would to get returned from the json-to-object-transformer either... any idea what might go wrong here?

        And how am I supposed to be able to pick up the transformed objects?

        My logging output for 1 request is this: (and I can clearly see the json returning)

        Code:
        INFO : be.tradelec.print.client.PrintClientUtil - Returning id:  1 at 2011-05-04 01:53:17
        DEBUG: org.springframework.integration.channel.DirectChannel - preSend on channel 'httpGatewayChannel', message: [Payload=1][Headers={timestamp=1304509997978, id=ca047014-fbde-4322-946a-92591a95b37c}]
        DEBUG: org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler - org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#0 received message: [Payload=1][Headers={timestamp=1304509997978, id=ca047014-fbde-4322-946a-92591a95b37c}]
        DEBUG: org.springframework.core.convert.support.GenericConversionService - Converting value 1 of java.lang.Object to java.lang.String
        DEBUG: org.springframework.core.convert.support.GenericConversionService - Converted to '1'
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - outboundHeaderNames=[Accept, Accept-Charset, Accept-Encoding, Accept-Language, Accept-Ranges, Authorization, Cache-Control, Connection, Content-Length, Content-Type, Cookie, Date, Expect, From, Host, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Max-Forwards, Pragma, Proxy-Authorization, Range, Referer, TE, Upgrade, User-Agent, Via, Warning]
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - headerName=[timestamp] WILL NOT be mapped
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - headerName=[id] WILL NOT be mapped
        DEBUG: org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8080/TradPrintServer/printqueue/1"
        DEBUG: org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, */*]
        DEBUG: org.springframework.web.client.RestTemplate - GET request for "http://localhost:8080/TradPrintServer/printqueue/1" resulted in 200 (OK)
        DEBUG: org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "application/json;charset=UTF-8" using [[email protected]a4cb]
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - inboundHeaderNames=[Accept-Ranges, Age, Allow, Cache-Control, Content-Encoding, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Type, Date, ETag, Expires, Last-Modified, Location, Pragma, Proxy-Authenticate, Refresh, Retry-After, Server, Set-Cookie, Trailer, Transfer-Encoding, Vary, Via, Warning, WWW-Authenticate]
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - headerName=[Server] WILL be mapped, matched pattern=Server
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - setting headerName=[Server], value=[Apache-Coyote/1.1]
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - headerName=[Content-Type] WILL be mapped, matched pattern=Content-Type
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - setting headerName=[Content-Type], value=application/json;charset=UTF-8
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - headerName=[Transfer-Encoding] WILL be mapped, matched pattern=Transfer-Encoding
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - setting headerName=[Transfer-Encoding], value=[chunked]
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - headerName=[Date] WILL be mapped, matched pattern=Date
        DEBUG: org.springframework.integration.http.support.DefaultHttpHeaderMapper - setting headerName=[Date], value=1.304.509.997.000
        DEBUG: org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler - handler 'org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#0' sending reply Message: [Payload={"id":1,"version":0,"queue":[{"name":"test1.pdf","id":1,"version":1},{"name":"test2,pdf","id":2,"version":0}],"printerId":1}][Headers={timestamp=1304509997990, id=74496882-b984-4b1a-a5aa-aa44b9c4c392, Date=1304509997000, Transfer-Encoding=chunked, http_statusCode=200, Content-Type=application/json;charset=UTF-8, Server=Apache-Coyote/1.1}]
        DEBUG: org.springframework.integration.channel.DirectChannel - preSend on channel 'replyChannel', message: [Payload={"id":1,"version":0,"queue":[{"name":"test1.pdf","id":1,"version":1},{"name":"test2,pdf","id":2,"version":0}],"printerId":1}][Headers={timestamp=1304509997990, id=74496882-b984-4b1a-a5aa-aa44b9c4c392, Date=1304509997000, Transfer-Encoding=chunked, http_statusCode=200, Content-Type=application/json;charset=UTF-8, Server=Apache-Coyote/1.1}]
        DEBUG: org.springframework.integration.transformer.MessageTransformingHandler - org.springframework.integration.transformer.MessageTransformingHandler#0 received message: [Payload={"id":1,"version":0,"queue":[{"name":"test1.pdf","id":1,"version":1},{"name":"test2,pdf","id":2,"version":0}],"printerId":1}][Headers={timestamp=1304509997990, id=74496882-b984-4b1a-a5aa-aa44b9c4c392, Date=1304509997000, Transfer-Encoding=chunked, http_statusCode=200, Content-Type=application/json;charset=UTF-8, Server=Apache-Coyote/1.1}]
        DEBUG: org.springframework.integration.handler.LoggingHandler - org.springframework.integration.handler.LoggingHandler#0 received message: [Payload={"id":1,"version":0,"queue":[{"name":"test1.pdf","id":1,"version":1},{"name":"test2,pdf","id":2,"version":0}],"printerId":1}][Headers={timestamp=1304509997990, id=74496882-b984-4b1a-a5aa-aa44b9c4c392, Date=1304509997000, Transfer-Encoding=chunked, http_statusCode=200, Content-Type=application/json;charset=UTF-8, Server=Apache-Coyote/1.1}]
        DEBUG: org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'replyChannel', message: [Payload={"id":1,"version":0,"queue":[{"name":"test1.pdf","id":1,"version":1},{"name":"test2,pdf","id":2,"version":0}],"printerId":1}][Headers={timestamp=1304509997990, id=74496882-b984-4b1a-a5aa-aa44b9c4c392, Date=1304509997000, Transfer-Encoding=chunked, http_statusCode=200, Content-Type=application/json;charset=UTF-8, Server=Apache-Coyote/1.1}]
        DEBUG: org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'httpGatewayChannel', message: [Payload=1][Headers={timestamp=1304509997978, id=ca047014-fbde-4322-946a-92591a95b37c}]
        All comments/suggestions more than welcome.

        Best wishes,

        Jochen
        Last edited by Dr.Drane; May 4th, 2011, 06:55 AM.

        Comment


        • #5
          You have two subscribers to a DirectChannel (P2P) which is your 'replyChannel' which means that based on your configuration the reply message goes to the "replyLogger":

          It is not illegal configuration. All it means is that it will load-balance between two subscribers following round-robin model (unless configured otherwise which is a separate subject).
          I think what you are looking for is pub-sub model, so change your 'replyChannel' configuration to
          Code:
          <int:publish-subscribe-channel id="replyChannel" />

          Comment


          • #6
            Actually, instead of using publish-subscribe-channel just to enable a "secondary" logging adapter, I would recommend using wire-taps for the logging since it's not part of the "primary" flow. You can connect logging-channel-adapters to the channel specified within the wire-tap interceptor. See this section of the reference manual for more detail: http://static.springsource.org/sprin...hannel-wiretap

            Comment

            Working...
            X