official site: http://camel.apache.org/sql-component.html
Camel Route Configuration:
address="${PublishEventUrl}" JNDI Way Configuration as like:
<Environment name="PublishEventUrl" type="java.lang.String" value="http://localhost:8100/dgig-mock/cxf/EventPublisherService/integration/v1/publishEvent"/>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder/> <bean id="integrationMessageWriter" class="com.exigen.dgig.message.IntegrationMessageWriter"/> <bean id="flowExtractor" class="com.exigen.dgig.message.FlowIdExtractor"> <property name="pattern"><value>\S+/integration/v1/flows/(\d+)</value></property> <property name="headerName" value="Location"/> </bean> <cxf:rsClient id="publishEventClient" address="${PublishEventUrl}" loggingFeatureEnabled="true"> <cxf:providers> <ref bean="integrationMessageWriter"/> </cxf:providers> </cxf:rsClient> <camelContext xmlns="http://camel.apache.org/schema/spring"> <onException> <exception>java.lang.Throwable</exception> <retryWhile> <constant>true</constant> </retryWhile> <redeliveryPolicy redeliveryDelay="1000" logRetryAttempted="true" retryAttemptedLogLevel="WARN"/> </onException> <route id="integrationMessageRoute"> <from uri="sql:SELECT * FROM IntegrationMessage WHERE messageId IS NULL ORDER BY id?dataSource=#dataSource&delay=1s"/> <log message="Found new message transactionId:${body[transactionId]}" loggingLevel="INFO"/> <log message="${body[messageBody]}" loggingLevel="TRACE"/> <setHeader headerName="dbMessageId"><simple>${body[id]}</simple></setHeader> <inOut uri="cxfrs:bean:publishEventClient"/> <log message="Message sent to DGIG NEXT layer. Received location: ${headers.Location}" loggingLevel="INFO"/> <setHeader headerName="messageId"><ref>flowExtractor</ref></setHeader> <to uri="sql:UPDATE IntegrationMessage SET messageId=:#messageId WHERE id=:#dbMessageId?dataSource=#dataSource"/> </route> </camelContext> </beans>
package com.exigen.dgig.message; import org.apache.cxf.jaxrs.utils.JAXRSUtils; import org.apache.cxf.message.Message; import org.apache.cxf.phase.PhaseInterceptorChain; import org.springframework.util.Assert; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.math.BigDecimal; import java.util.Map; import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE; /** * This class creates HTTP request using {@link com.exigen.dgig.message.domain.IntegrationMessage}. * It adds UTF8 encoding to header and writes MESSAGEBODY header as body. */ @Provider @Produces({"application/xml", "application/*+xml", "text/xml"}) public class IntegrationMessageWriter implements MessageBodyWriter<Map<String, ?>> { private static final String UTF_8 = "UTF-8"; private static final String CHARSET = "charset"; private static final String MESSAGE_BODY = "MESSAGEBODY"; @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Map.class.isAssignableFrom(type); } @Override public long getSize(Map<String, ?> integrationMessage, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } /** * @param integrationMessage map which keys represent {@link com.exigen.dgig.message.domain.IntegrationMessage} fields. * @param httpHeaders which can be set or modified; * @param entityStream stream to write request body. */ @Override public void writeTo(Map<String, ?> integrationMessage, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { Assert.isTrue(integrationMessage.containsKey(MESSAGE_BODY), "No " + MESSAGE_BODY + " parameter found."); Message message = PhaseInterceptorChain.getCurrentMessage(); httpHeaders.putSingle(CONTENT_TYPE, JAXRSUtils.mediaTypeToString(mediaType, CHARSET) + ';' + CHARSET + "=" + UTF_8); final BigDecimal id = (BigDecimal) integrationMessage.get("ID"); final byte[] body = (byte[]) integrationMessage.get(MESSAGE_BODY); entityStream.write(body); } }
浙公网安备 33010602011771号