105 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package com.release11;
 | |
| 
 | |
| import com.mysql.cj.jdbc.MysqlDataSource;
 | |
| import org.apache.activemq.ActiveMQConnectionFactory;
 | |
| import org.apache.camel.*;
 | |
| import org.apache.camel.builder.RouteBuilder;
 | |
| import org.apache.camel.component.jms.JmsComponent;
 | |
| import org.apache.camel.impl.DefaultCamelContext;
 | |
| import org.apache.camel.support.SimpleRegistry;
 | |
| import org.apache.log4j.BasicConfigurator;
 | |
| import org.apache.camel.converter.jaxb.JaxbDataFormat;
 | |
| import javax.xml.bind.*;
 | |
| import java.io.File;
 | |
| import java.io.FileInputStream;
 | |
| import java.io.InputStream;
 | |
| import java.nio.file.Files;
 | |
| import java.nio.file.Path;
 | |
| import java.nio.file.Paths;
 | |
| import java.util.ArrayList;
 | |
| import java.util.List;
 | |
| 
 | |
| 
 | |
| public class Main {
 | |
| 
 | |
|     public static void main(String[] args) throws Exception {
 | |
| 
 | |
|         //to("log:?level=INFO&showHeaders=true&showBody=true")
 | |
| 
 | |
|         MysqlDataSource source = new MysqlDataSource();
 | |
|         String jacek = "jdbc:mysql://10.101.111.19:3306/camel_db";
 | |
|         source.setURL(jacek);
 | |
|         source.setUser("root");
 | |
|         source.setPassword("admin");
 | |
|         SimpleRegistry registry = new SimpleRegistry();
 | |
|         registry.bind("source", MysqlDataSource.class, source);
 | |
| 
 | |
|         CamelContext context = new DefaultCamelContext(registry);
 | |
|         BasicConfigurator.configure();
 | |
|         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.101.111.19:8088");
 | |
|         connectionFactory.setUserName("admin");
 | |
|         connectionFactory.setPassword("admin");
 | |
|         context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
 | |
| 
 | |
|         context.addRoutes(new RouteBuilder() {
 | |
|             @Override
 | |
|             public void configure() throws Exception {
 | |
| 
 | |
|                 JaxbDataFormat xmlDataFormat = new JaxbDataFormat();
 | |
|                 xmlDataFormat.setContextPath("generated");
 | |
|                 //xmlDataFormat.setSchemaLocation("/home/igor/Documents/Jacek/jacek-and-igor/src/main/resources/material.xsd");
 | |
| 
 | |
|                 from("direct:start")
 | |
|                         .setBody(constant("SELECT * FROM material"))
 | |
|                         .to("jdbc:source")
 | |
|                         .split(body())
 | |
|                         .to("activemq:queue:RawMaterial");
 | |
| 
 | |
|                 from("direct:getPackages")
 | |
|                         .setHeader("material_id", simple("${body[id]}"))
 | |
|                         .setBody(simple("SELECT * FROM package WHERE material_id = :?material_id"))
 | |
|                         .to("jdbc:source?useHeadersAsParameters=true");
 | |
| 
 | |
|                 from("activemq:queue:RawMaterial")
 | |
|                         .enrich("direct:getPackages", new MergeAggregator())
 | |
|                         .process(new ValidatorProcess())
 | |
|                         .choice()
 | |
|                         .when(simple("${exchangeProperty[dimension]}"+" == false"))
 | |
|                         .marshal(xmlDataFormat)
 | |
|                         .to("activemq:queue:BadEan")
 | |
|                         .when(simple("${exchangeProperty[ean]}"+" == false"))
 | |
|                         .marshal(xmlDataFormat)
 | |
|                         .to("activemq:queue:BadDimension")
 | |
|                         .otherwise()
 | |
|                         .marshal(xmlDataFormat)
 | |
|                         .to("activemq:queue:ValidMaterial")
 | |
|                         .to("log:?level=INFO&showBody=true");
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|                 from("activemq:queue:siema")
 | |
|                         .process(new XMLProcess())
 | |
|                         .unmarshal(xmlDataFormat)
 | |
|                         .process(new XMLProcess())
 | |
|                         .to("activemq:queue:test");
 | |
| 
 | |
|             }
 | |
|         });
 | |
| 
 | |
|         context.start();
 | |
|         ProducerTemplate template = context.createProducerTemplate();
 | |
| 
 | |
|         Path path = Paths.get("/home/igor/Documents/Jacek/jacek-and-igor/src/main/resources/test.xml");
 | |
|         ArrayList<String> readAllLines = (ArrayList<String>) Files.readAllLines(path);
 | |
|         String allFile = "";
 | |
|         for (int i = 0; i < readAllLines.size(); i++) {
 | |
|             allFile+=readAllLines.get(i);
 | |
|         }
 | |
| 
 | |
|         //template.sendBody("direct:start", null);
 | |
|         template.sendBody("activemq:queue:siema", allFile);
 | |
| 
 | |
|         context.stop();
 | |
|     }
 | |
| } |