After how camel used to retrieve files from and transfer files from ftp in a Project Integration Project last time, another application we often encounter in system integration is to transmit data to the queue of message-oriented middleware through JMS or retrieve messages from the queue of message-oriented middleware.
This article briefly introduces and provides an example of a requirement to use camel to monitor whether a folder contains files and send the files to the queue monitored by another system. (The image is from camel in action)
1, because to use JMS, Here introduces an Open Source activemq, can be downloaded from the http://activemq.apache.org/download.html, download and decompress, bin directory has an activemq. BAT file, run activemq in the command line to start activemq. If you can access http: // localhost: 8161/admin/from a browser, activemq is successfully started.
2. Implement the routing shown in camel: in the Java project, configure the jar package of activemq to classpath. the Java code is as follows:
private static String user = ActiveMQConnection.DEFAULT_USER; private static String password = ActiveMQConnection.DEFAULT_PASSWORD; private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String args[]) throws Exception { CamelContext context = new DefaultCamelContext(); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); System.out.println(url + " " + user + password); context.addRoutes(new RouteBuilder() { public void configure() { from("file:d:/temp/inbox").to( "jms:queue:TOOL.DEFAULT"); } }); context.start(); boolean loop = true; while (loop) { Thread.sleep(25000); } context.stop(); }
Camel sends the file content to the queue named 'tool. default' in activemq as binary message during routing.
Of course, you can also use the following method to send textmessage: From ("file: D:/temp/inbox "). convertbodyto (string. class ). to ("JMS: queue: tool. default ");
The following code can be used to retrieve messages from the queue sent by camel.
private static String user = ActiveMQConnection.DEFAULT_USER; private static String password = ActiveMQConnection.DEFAULT_PASSWORD; private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; private static boolean transacted; private static int ackMode = Session.AUTO_ACKNOWLEDGE; public static void main(String[] args) throws Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(transacted, ackMode); Destination destination = session.createQueue("TOOL.DEFAULT"); MessageConsumer consumer = session.createConsumer(destination); Message message = consumer.receive(1000); if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; System.out.println("Received Text message : " + txtMsg.getText()); } else if(message != null){ BytesMessage bytesMsg = (BytesMessage) message; byte[] bytes = new byte[(int) bytesMsg.getBodyLength()]; bytesMsg.readBytes(bytes); System.out.println("Received byte message: " + new String(bytes)); } consumer.close(); session.close(); connection.close(); }
Similarly, the preceding routing can also be implemented through spring Configuration:
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="failover://tcp://localhost:61616"/> </bean> </property> </bean> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="file:d:/temp/inbox"/> <to uri="jms:queue:TOOL.DEFAULT"/> </route> </camelContext>