You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicemix.apache.org by atr <an...@t-systems.com> on 2009/11/25 16:02:51 UTC

does not delete files after processing

Hello.

I have a problem with a file:poller based transformation: My incoming file
won't be deleted while processing.
Why the file will not be deleted?
I use Servicemix 3.3.. Below you'll find my configurations and also my
transformation Java-Bean.
I have no errors in my log file and the transformation is done well.

By the way, I have migrated from Servicemix 3.2.2 to Version 3.3 and changed
in the xbean.xml-Heead the version of XSD-Schema for servicemix-file and
servicemix-eip. But this change does not solve the problem.

Her you find my code:

File-Poller-XBean
==============================================
==============================================
<beans xmlns:file="http://servicemix.apache.org/file/1.0"
       xmlns:sportscheck="http://sportscheck.com/services/orders"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/file/1.0
http://servicemix.apache.org/schema/servicemix-file-2008.01.xsd
       http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  
	<file:poller service="sportscheck:OrdersInProduction"
		endpoint="endpoint" 
		targetService="sportscheck:eip"
		file="${demandware.export.production.orders}"
		recursive="true"
		period="${orders.dw.export.210.filepoller}"
		filter="#XMLFilter"
		deleteFile="true">
	</file:poller>

	<file:sender service="sportscheck:OrdersOutProduction"
		endpoint="endpoint"
		directory="${baur.import.production.orders}">
	</file:sender>    
 
 	<file:poller service="sportscheck:OrdersInTest"
		endpoint="endpoint"
		targetService="sportscheck:eip"
		file="${demandware.export.test.orders}"
		recursive="true"
		period="${orders.dw.export.210.filepoller}"
		filter="#XMLFilter">
	</file:poller>
	 
  	<file:sender service="sportscheck:OrdersOutTest"
		endpoint="endpoint"
		directory="${baur.import.test.orders}">
	</file:sender>    

    <file:sender service="sportscheck:archive"
        endpoint="endpoint"
        directory="${archive.orders}">
    </file:sender>
 	<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
      	<list>
         <value>classpath:pathes.properties</value>
         <value>classpath:my.properties</value>
        </list>
      </property>
	</bean>

	<bean id="XMLFilter"
class="de.mmsdresden.sportscheck.utils.RecursiveFileFilter">
		<constructor-arg value=".xml" />
	</bean>
</beans>


EIP-XBean
==============================================
==============================================
<beans xmlns:eip="http://servicemix.apache.org/eip/1.0"
       xmlns:sportscheck="http://sportscheck.com/services/orders"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/eip/1.0
                          
http://servicemix.apache.org/schema/servicemix-bean-2008.01.xsd   
                           http://www.springframework.org/schema/beans
                          
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<eip:static-recipient-list service="sportscheck:eip" endpoint="endpoint">
		<eip:recipients>
			<eip:exchange-target service="sportscheck:orderBean" />
			<eip:exchange-target service="sportscheck:archiveBean" />
		</eip:recipients>
	</eip:static-recipient-list>

</beans>


Bean-XBean
==============================================
==============================================
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:sportscheck="http://sportscheck.com/services/orders"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/bean/1.0
http://servicemix.apache.org/schema/servicemix-bean-2008.01.xsd   
       http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean:endpoint service="sportscheck:orderBean"
		endpoint="endpoint" bean="#OrderBean" />

	<bean id="OrderBean" class="de.mmsdresden.sportscheck.beans.OrderBean" />
	
	<bean:endpoint service="sportscheck:archiveBean"
		endpoint="endpoint" bean="#ArchiveBean" />

	<bean id="ArchiveBean" class="de.mmsdresden.sportscheck.beans.ArchiveBean"
/>

</beans>


Java-Bean
==============================================
==============================================
package de.mmsdresden.sportscheck.beans;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.jbi.component.ComponentContext;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.jbi.messaging.MessageExchange.Role;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.servicemix.MessageExchangeListener;
import org.apache.servicemix.client.ServiceMixClientFacade;
import org.apache.servicemix.components.util.DefaultFileMarshaler;
import org.apache.servicemix.jbi.api.Destination;
import org.apache.servicemix.jbi.api.ServiceMixClient;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.milyn.payload.JavaResult;
import org.xml.sax.SAXException;

import de.mmsdresden.sportscheck.pojos.orders.bfs.output.BFSBusinessCase;
import de.mmsdresden.sportscheck.pojos.orders.demandware.input.Order;
import de.mmsdresden.sportscheck.utils.FileNameUtils;
import de.mmsdresden.sportscheck.utils.Mail;
import de.mmsdresden.sportscheck.utils.MessageExchangeCache;
import de.mmsdresden.sportscheck.utils.MessageExchangeCacheImpl;
import de.mmsdresden.sportscheck.utils.OrderTransformator;
import de.mmsdresden.sportscheck.utils.ServiceMixUtils;
import de.mmsdresden.sportscheck.utils.SmooksWrapper;
import de.mmsdresden.sportscheck.utils.XMLUtils;
import de.mmsdresden.sportscheck.utils.properties.PropertyLoader;
import
de.mmsdresden.sportscheck.utils.properties.factories.FilenamePropertyFactory;
import
de.mmsdresden.sportscheck.utils.properties.factories.MailPropertyFactory;

/**
 * DW -> BFS Order Mapping
 * 
 * @author Andreas Trappe
 * @author Andreas Lutz
 * @since 13.07.2009
 */

public class OrderBean implements MessageExchangeListener {
	/**
	 * @see javax.jbi.messaging.DeliveryChannel
	 */
	@Resource
	private DeliveryChannel channel;

	/**
	 * @see javax.jbi.component.ComponentContext
	 */
	@Resource
	private ComponentContext context;

	/**
	 * @see org.apache.servicemix.client.ServiceMixClient
	 */
	private ServiceMixClient client;

	private Destination fileoutProduction;

	private Destination fileoutTest;

	private static Log LOGGER = LogFactory.getLog(OrderBean.class);

	private static final String SERVICE_NAMESPACE =
"service:http://sportscheck.com/services/orders/";

	private static final String FILEOUT_PRODUCTION_ENDPOINT = SERVICE_NAMESPACE
+ "OrdersOutProduction";

	private static final String FILEOUT_TEST_ENDPOINT = SERVICE_NAMESPACE +
"OrdersOutTest";

	private static final String SMOOKS_ORDER_INPUT_CONF =
"smooks_conf/orders_dmware_input.xml";

	private static final String SMOOKS_ORDER_OUTPUT_CONF =
"smooks_conf/orders_bfs_output.xml";

	private MessageExchangeCache exchangeCache = new
MessageExchangeCacheImpl();

	private PropertyLoader propsFilename = null;

	private String fileInName;

	private String fileOutName;

	private static final String PROPERTIES_PATH_TEST =
"baur.import.test.orders";

	private static final String PROPERTIES_PATH_PRODUCTION =
"baur.import.production.orders";

	private static final String DEFAULT_XSL_PATH = "sportscheck_conf/XSL/";

	private static final String DW_XSL = "DW/dw_orders.xsl";

	private static String GLOBAL_ERROR;

	private static String GLOBAL_PROCESS_INVALID_FILES;

	static javax.xml.transform.TransformerFactory factory =
javax.xml.transform.TransformerFactory.newInstance();

	// some Mail constants //
	private static String MAIL_SENDMAIL;

	private static String MAIL_SENDER;

	private static String MAIL_RECEIVERS;

	private static String[] MAIL_RECEIVERS_ARRAY;

	private static Mail MAIL;

	private static PropertyLoader props = null;
	static {
		try {
			props = new MailPropertyFactory().getImplementation();
		} catch (FileNotFoundException e) {
			LOGGER.error(e);
		} catch (IOException e) {
			LOGGER.error(e);
		}
		GLOBAL_PROCESS_INVALID_FILES =
props.getValue("global.processInvalidFiles");
		MAIL = new Mail();
		MAIL.setSmtpHost(props.getValue("smtp_host"), props.getValue("smtp_user"),
props.getValue("smtp_pass"));
		MAIL_SENDMAIL =
props.getValue("esb.orders.validateGeneratedXML.invalid.sendmail");
	}

	/**
	 * This init method checks that the needed artifacts are initialized
	 * 
	 * @throws IOException
	 * @throws FileNotFoundException
	 * @throws MessagingException
	 */
	@PostConstruct
	public void init() throws FileNotFoundException, IOException,
MessagingException {
		if (this.context == null || this.channel == null) {
			throw new IllegalStateException("Bean not initialized");
		}
		this.propsFilename = new FilenamePropertyFactory().getImplementation();
		this.fileOutName = this.propsFilename.getValue("orders");

		/*
		 * Create client to get the destination (where to send the files)
		 */
		this.client = new ServiceMixClientFacade(this.context);

		this.fileoutProduction =
this.client.createDestination(FILEOUT_PRODUCTION_ENDPOINT);
		this.fileoutTest = this.client.createDestination(FILEOUT_TEST_ENDPOINT);

		LOGGER.info("-----------------------------------------------");
		LOGGER.info("--------- Order mapping initialized -----------");
		LOGGER.info("-----------------------------------------------");
		LOGGER.info("-> For more informations turn on debug logging.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.servicemix.MessageExchangeListener#onMessageExchange(javax
	 * .jbi.messaging.MessageExchange)
	 */
	public synchronized void onMessageExchange(MessageExchange exchange) throws
MessagingException {
		LOGGER.debug("Received Exchange: " + exchange.getExchangeId());

		if (exchange.getRole().equals(Role.CONSUMER)) {
			if (this.exchangeCache.containsKey(exchange.getExchangeId())) {
				boolean done = true;
				boolean error = false;
				LOGGER.debug("Exchange has status: " + exchange.getStatus());
				if (exchange.getStatus().equals(ExchangeStatus.ACTIVE))
					done = false;
				if (exchange.getStatus().equals(ExchangeStatus.ERROR))
					error = true;
				if (done && !error) {
					LOGGER.debug("Setting exchange: " + exchange.getExchangeId() + " to
DONE!");
					ServiceMixUtils.sendMessage(exchange, ExchangeStatus.DONE,
this.channel);
					this.exchangeCache.removeFromCache(exchange.getExchangeId());
				}
			}
		} else {
			try {
				process(exchange);
			} catch (IOException e) {
				LOGGER.error(e);
			} catch (SAXException e) {
				LOGGER.error(e);
			} catch (TransformerException e) {
				LOGGER.error(e);
			}
		}
		LOGGER.debug("Exchanges in cache: " + this.exchangeCache.size());
	}

	private void process(MessageExchange exchange) throws TransformerException,
IOException, SAXException, MessagingException {
		// process message //
		InOnly inExchange = (InOnly) exchange;
		NormalizedMessage message = inExchange.getInMessage();
		String filePath = (String)
message.getProperty(DefaultFileMarshaler.FILE_PATH_PROPERTY);
		this.fileInName = (String)
message.getProperty(DefaultFileMarshaler.FILE_NAME_PROPERTY);
		StreamSource content = ServiceMixUtils.getMessagePayload(message);
		LOGGER.info("Processing Exchange Message, with file name " + fileInName);

		// fix DW XML (Smooks only works with unique tag-ids) //
		LOGGER.info("Fixing DW XML file ...");
		content = fixedXML(content);

		// get XML into JAVA //
		LOGGER.info("Mapping XML to Java ...");
		List<Order> in_orders = xmlTojava(content);

		// process JAVA with business logic
		LOGGER.info("Processing Java to Java ...");
		LinkedList<BFSBusinessCase> transformedOrders = javaTojava(in_orders);

		// create XML from JAVA //
		LOGGER.info("Creating XML from Java ...");
		StreamResult out = javaToxml(transformedOrders);

		// validate generated xml //
		LOGGER.info("Validating generated XML ...");
		boolean isvalid = validate(filePath, out);
		if (!isvalid && MAIL_SENDMAIL.equals("1")) {
			sendErrorMail(filePath);
		}

		// ouput XML //
		if (isvalid || (!isvalid && GLOBAL_PROCESS_INVALID_FILES.equals("1")))
			outputToFile(filePath, out);

		// Done //
		LOGGER.info("Done processing exchange message ...");
	}

	private StreamSource fixedXML(StreamSource content) throws IOException,
TransformerConfigurationException, FileNotFoundException {
		SAXBuilder sb = new SAXBuilder();
		try {
			Document doc = sb.build(content.getInputStream());
			Element root = doc.getRootElement();
			Element transformed = XMLUtils.modifyXML(root,
System.getProperty("user.dir") + File.separator + DEFAULT_XSL_PATH +
DW_XSL);

			XMLOutputter xo = new XMLOutputter();
			// xo.output(transformed, new FileWriter("c:\\test.xml"));
			return new StreamSource(new
ByteArrayInputStream(xo.outputString(transformed).getBytes()));

		} catch (JDOMException e) {
			LOGGER.error("Error parsing XML JDOM - " + e);
			return null;
		}
	}

	private StreamResult javaToxml(LinkedList<BFSBusinessCase>
transformedOrders) throws IOException, SAXException {
		SmooksWrapper xmlMapper = SmooksWrapper.getInstance();
		Map<String, Object> outObjects = new HashMap<String, Object>();
		outObjects.put("transformedOrders", transformedOrders);
		StreamResult out = xmlMapper.mapJavaToXML(SMOOKS_ORDER_OUTPUT_CONF,
outObjects);
		return out;
	}

	private LinkedList<BFSBusinessCase> javaTojava(List<Order> in_orders) {
		OrderTransformator transformer = new OrderTransformator(in_orders, true,
fileInName);
		transformer.transform();
		LinkedList<BFSBusinessCase> transformedOrders = transformer.getResult();
		return transformedOrders;
	}

	@SuppressWarnings("unchecked")
	private List<Order> xmlTojava(StreamSource content) throws IOException,
SAXException {
		SmooksWrapper xmlMapper = SmooksWrapper.getInstance();
		JavaResult result = xmlMapper.mapXMLtoJava(SMOOKS_ORDER_INPUT_CONF,
content);
		List<Order> in_orders = (LinkedList<Order>) result.getBean("orders");
		return in_orders;
	}

	private boolean validate(String filePath, StreamResult out) {
		String path = null;
		if (ServiceMixUtils.isTestDir(filePath))
			path = PROPERTIES_PATH_TEST;
		else
			path = PROPERTIES_PATH_PRODUCTION;
		try {
			XMLUtils.validateXML(out.getOutputStream(), path);
		} catch (Exception e) {
			LOGGER.error("Exception: " + e);
			GLOBAL_ERROR = e.getMessage();
			return false;
		}
		return true;
	}

	private void sendErrorMail(String filePath) {
		MAIL_SENDER =
props.getValue("esb.orders.validateGeneratedXML.invalid.sender");
		MAIL_RECEIVERS =
props.getValue("esb.orders.validateGeneratedXML.invalid.receivers");
		// split receivers //
		if (MAIL_RECEIVERS.indexOf("|") > -1) {
			MAIL_RECEIVERS_ARRAY = MAIL_RECEIVERS.split("\\|");
		} else {
			MAIL_RECEIVERS_ARRAY = new String[1];
			MAIL_RECEIVERS_ARRAY[0] = new String(MAIL_RECEIVERS);
		}
		MAIL.setContent(MAIL_SENDER, "Error while validating generated Baur order
file");
		MAIL.addRecipients(MAIL_RECEIVERS_ARRAY);
		MAIL.addText("Error validating " + filePath + ":");
		MAIL.addText(GLOBAL_ERROR);
		if (!MAIL.sendMail()) {
			LOGGER.error("Could not sent mail.\r\n Error: " + MAIL.lastError);
		}
	}

	private void outputToFile(String filePath, StreamResult out) throws
MessagingException, IOException {
		LOGGER.info("Saving XML to Filesystem ...");

		MessageExchange[] exchanges = null;

		if (ServiceMixUtils.isTestDir(filePath)) {
			exchanges = new MessageExchange[1];

			// fileout test copy //
			Properties fileOutProps = new Properties();
			String fileOutTotal =
FileNameUtils.getOutFilenameWithUniqueKey(this.fileOutName, fileInName,
".xml");
			fileOutProps.put(DefaultFileMarshaler.FILE_NAME_PROPERTY, fileOutTotal);
			exchanges[0] = ServiceMixUtils.sendMessage(this.fileoutTest, out,
fileOutProps, this.client);
			LOGGER.debug("Exchange created: " + exchanges[0].getExchangeId() + ",
with file name " + fileOutTotal);

		} else {
			exchanges = new MessageExchange[1];

			// fileout production copy //
			Properties fileOutProps = new Properties();
			String fileOutTotal =
FileNameUtils.getOutFilenameWithUniqueKey(this.fileOutName, fileInName,
".xml");
			fileOutProps.put(DefaultFileMarshaler.FILE_NAME_PROPERTY, fileOutTotal);
			exchanges[0] = ServiceMixUtils.sendMessage(this.fileoutProduction, out,
fileOutProps, this.client);
			LOGGER.debug("Exchange created - Production file: " +
exchanges[0].getExchangeId() + ", with file name " + fileOutTotal);
		}
	}
}

-- 
View this message in context: http://old.nabble.com/%3Cfile%3Apolller%3E-does-not-delete-files-after-processing-tp26514183p26514183.html
Sent from the ServiceMix - Dev mailing list archive at Nabble.com.