Everything about Hot Folder in Hybris
1. Overview
The hot folder is one of the best ways to import and feed data into Hybris, it is based on Spring integration framework.
Some reasons to use hot folder :
- Easy to configure and to extend.
- Use the power of Spring integration.
- It’s very fast than the regular impex import (uses multi-threading imports).
Basically the main idea behind hot folders is to transform csv files giving in input to impexes using a pre-configured ImpexConverters
and import them using ImportService
(see diagram bellow).
This is a more complete version of how the hot folder works.
Let’s explain this diagram step by step 🙂
- Inbound Channel Adapter : is a Spring integration component that allows us to create a watcher over csv files on a specific directory.
- File Order Comparator : the Inbound Channel Adapter uses a file order comparator to treat files by order of priority, this comparator compares files based on their file names.
- Header Setup Task : in this step of the flow a
BatchHeader
is created/initialized with the file, catalog, and some other information, thisBatchHeader
will be used throughout the flow as a reference for file and some other information. - Header Init Task : this is just another initialization step, in this step we extract the
sequenceId
and thelanguage
then we add them to theBatchHeader
for later use.
For example for this file name product-fr-2313213672186.csv thesequenceId
is 2313213672186 and thelanguage
is fr. - Impex Transformer Task : this is one of the important steps in the flow, basically here where the original file (csv) is converted to an impex with the help of a pre-configured
ImpexConverter
. - Impex Runner Task : imports the transformed file (impex) using
ImportService.importData()
method. - Cleanup Task : deletes the transformed file (impex) and archive the original file (csv).
The majority of the components that constitute the hot folder flow can be found at : …\hybris\bin\ext-accelerator\acceleratorservices\resources\acceleratorservices\integration\hot-folder-spring.xml
2. Implementation
Fortunately, the accelerator comes armed with an initial and complete configuration of the hot folder.
The ImpexConverters
for prices, products, medias, stocks, customers…, are already there and good to go.
However in this article, we will try to extend the hot folder functionalities to be able to import an other item type UnitModel
.
You need three macro steps to configure the hot folder :
- Define a base directory where the csv files will be put.
- Initiate your flow with the catalog and base directory…
- Create an
ImpexConverter
and associated with aMappingConverter
.
2.1. Preparation
1. Create an xml file hot-folder-store-training-spring.xml inside the …\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration\hot-folder-store-training-spring.xml -->
<?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:int="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
2. Import the hot-folder-store-training-spring.xml file to the trainingcore-spring.xml.
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore-spring.xml -->
<!-- Spring Integration -->
<import resource="classpath:/trainingcore/integration/hot-folder-store-training-spring.xml"/>
<import resource="classpath:/trainingcore/integration/hot-folder-store-electronics-spring.xml"/>
<import resource="classpath:/trainingcore/integration/hot-folder-store-apparel-spring.xml"/>
<import resource="classpath:/trainingcore/integration/hot-folder-common-spring.xml"/>
2.2. Initial Config
1. Inside the hot-folder-store-training-spring.xml add a base directory and an inbound-channel-adapter
to watch over the base directory.
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration\hot-folder-store-training-spring.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns... >
<context:annotation-config/>
<!-- Config a base directory -->
<bean id="baseDirectoryTraining" class="java.lang.String">
<constructor-arg value="#{baseDirectory}/${tenantId}/training" />
</bean>
<!-- Scan for files inside the base directory with names matches the pattern ^(.*)-(\d+)\.csv -->
<file:inbound-channel-adapter id="batchFilesTraining" directory="#{baseDirectoryTraining}"
filename-regex="^(.*)-(\d+)\.csv"
comparator="fileOrderComparator">
<!-- Periodic trigger in milliseconds -->
<int:poller fixed-rate="1000" />
</file:inbound-channel-adapter>
</beans>
The
#{baseDirectory}
by default is ${HYBRIS_DATA_DIR}/acceleratorservices/import, you can re-define it with the property :acceleratorservices.batch.impex.basefolder
2. Add an outbound-gateway
to move the received file to processing and invoke the first step of the flow.
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration\hot-folder-store-training-spring.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns... >
<context:annotation-config/>
...
<!-- Move the file to processing and start the flow -->
<file:outbound-gateway request-channel="batchFilesTraining" reply-channel="batchFilesTrainingProc"
directory="#{baseDirectoryTraining}/processing"
delete-source-files="true" />
</beans>
3. Create a service-activator
which is the first step of the flow, it feeds the flow with the catalog and other relevant information.
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration\hot-folder-store-training-spring.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns... >
<context:annotation-config/>
...
<!-- Initialize the batch header with relevant information -->
<int:service-activator input-channel="batchFilesTrainingProc" output-channel="batchFilesHeaderInit"
ref="trainingHeaderSetupTask"
method="execute" />
<bean id="trainingHeaderSetupTask" class="de.hybris.platform.acceleratorservices.dataimport.batch.task.HeaderSetupTask">
<property name="catalog" value="trainingProductCatalog" />
<property name="net" value="false" />
<property name="storeBaseDirectory" ref="baseDirectoryTraining" />
</bean>
</beans>
2.3. Create ImpexConverter
and ConverterMapping
1. Create an ImpexConvert
for the UnitModel
.
The ImpexConverter
has two properties a header
and an impexRow
:
- header : will be the header of the generated impex.
- impexRow : defines the mapping between the column of the csv file and the impex file.
Inside the hot-folder-store-training-spring.xml, create a Spring bean from DefaultImpexConverter
, with :
- header :
INSERT_UPDATE Unit ;unitType[unique=true] ;code[unique=true] ;name[lang=$lang] ;conversion
- impexRow :
;{+0} ;{+1} ;{2} ;{3}
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration\hot-folder-store-training-spring.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns... >
<context:annotation-config/>
...
<!-- UnitModel impex converter -->
<bean id="unitConverter" class="de.hybris.platform.acceleratorservices.dataimport.batch.converter.impl.DefaultImpexConverter">
<property name="header">
<value>
#{defaultImpexProductHeader}
INSERT_UPDATE Unit;unitType[unique=true];code[unique=true];name[lang=$lang];conversion
</value>
</property>
<property name="impexRow">
<value>;{+0};{+1};{2};{3}</value>
</property>
</bean>
</beans>
This means that :
{+0}
means that column 0 will be mapped tounitType[unique=true]
{+1}
means that column 1 will be mapped tocode[unique=true]
{2}
means that column 2 will be mapped toname[lang=$lang]
{3}
means that column 3 will be mapped toconversion
2. Map the unitConverter
to a file name prefix using DefaultConverterMapping
.
<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore\integration\hot-folder-store-training-spring.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns... >
<context:annotation-config/>
...
<!-- UnitModel impex converter mapping to unit prefix -->
<bean id="unitConverterMapping"
class="de.hybris.platform.acceleratorservices.dataimport.batch.converter.mapping.impl.DefaultConverterMapping"
p:mapping="unit"
p:converter-ref="unitConverter"/>
</beans>
This means that all the csv files start with unit will be treated using the unitConverter
, example unit-fr-1234567.csv.
3. Run an ant all
command then restart the server and you are good to go.
3. Hot Folder in Action
To test what you have accomplished so far, put a csv file that respect the impexRow
format and the p:mapping="unit"
inside the : ${HYBRIS_DATA_DIR}\acceleratorservices\import\master\training
Example : unit-en-123456789.csv
toto, toto, toto, 1
If everything goes well a Unit instance with code toto and name toto should be created.
Find a complete example on GitHub.
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
Great post easy to understand.
A question please what does the
+
means for{+0}
?The
+
means that this row is mandatory and cannot be empty.Is there any way to hardcode values while configuring hotfolder?
I don’t understand your question, sorry!
I think you could achieve that by modifying the header of Impex. e.g setting the language of the session for all CronJobs to english (I add default=en so if row do not specify a language for session it will have en by default)
INSERT_UPDATE CronJob ; code[unique=true]; job(code);sessionLanguage(isoCode)[default=en]
I want to send an email with error message if anything wrong while running hot folder (import error). can you please help me.
You have to override the
de.hybris.platform.acceleratorservices.dataimport.batch.task.ErrorHandler
and do what ever you need inside thepublic void onError(...)
method.Do we have mechanism to rollback all record if any point we face error during hot-folder import.
no impex is not transactionnal, all lines rejected are stored in a log file you can correct them and re-import
What do you mean override ErrorHandler?
hi shiva did u implement this email with error message while running hot folder import errorr
Easy to understand.
but, where does these values comes from #{defaultImpexProductHeader}.
You can find it here : ${HYBRIS_BIN_DIR}\bin\ext-accelerator\acceleratorservices\resources\acceleratorservices\integration\hot-folder-spring.xml
<bean id="defaultImpexProductHeader" class="java.lang.String">
<constructor-arg>
<value># ImpEx for importing data into $CATALOG$
$catalogVersion=catalogversion(catalog(id[default=$CATALOG$]),version[default='Staged'])[unique=true]
$approved=approvalstatus(code)[default='check']
$lang=$LANGUAGE$</value>
</constructor-arg>
</bean>
May I know how would it resolve value to a particular catalog like catalog = powertoolsProductCatalog?
I think I got my answer. It is in service activator part.
Hi Bro,
The content and the way of explanation is very good. But the text color is too light and a bit hard to read. Thanks for clear explanation.
Thanks,
Manoj
Thanks for your feedback!
Good post, can you also please let us know how to have bean configuration in xml config file so that service activator can call the particular bean and in the class i could have the file read and manipulate it.
Re-check the part where I created the
trainingHeaderSetupTask
bean.Can we do the deletion operation using hot folder like “Delete from {table_name}”
Yes, in the
header
of theDefaultImpexConverter
, addREMOVE [YourItemType]; code[unique=true];...
Hi! Can you please provide some information about how to pass « & » with the impex.
INSERT_UPDATE PointOfService;name[unique=true];type(code);address(&addrID);latitude;
I am getting this error » The reference to entity « addrID » must end with the ‘;’ delimiter »
Actually I want to add it through hot-folder in a csv file.
Hi Mouad thanks for the informarion I have a question.How to create the “trainingcore” directory and its internal directories?
The character ç at the end of the filename unit-en-12345678ç.csv is INCORRECT. It cause file isn’t loaded.
Oops! my bad, I fixed it.
Thanks Antonio 🙂
Please make a video on this session, starting from feeding CSV file as input To storing it to DB.
This will help us so much for understanding,
Hi Fakir, nice post for easy understanding.
Please post a detail configuration setup on V2 Hot-Folder (Cloud) from Hybris system to Microsoft Azure Storage Explorer.
Hi Mouad,
I have implemented Hot Folder Functionality seeing your example. In Our case we have ; as delimeter for csv file and it works fine if all coulmns have singular value.
I am facing issue for columns who are of list or collection types. I tried like ex. “A,B,C” for same but it also takes , as seperator,
Any suggestion would be helpful.
You have to use this syntax on header -> CollectionAttribute[separator = :]
And mak a value like A:B:C separated with : instead of , since it’s used for your csv
A very basic and excellent blog of explanation which can help alot to a biginner.
Hi,
How can we convert json data getting from restful api to csv files to implement hot batch folder concept
You can use a job that call the api and get data then convert it to a csv file (there’s many lib to do that) then send the result file to hotfolder.
Hi,
I’m getting below error in error folder
Missing value for 1: kw1;kws1;;
I/P file contain
;kw;kws;Kilowatt;
Thanks in advance
Could you provide the configuration for this particular import ?
I have use ; in CSV as a Field separator
But default is , comma
Resolved my issue now.
Thanks
Good
Thanks for your feedback!
Hi @Mouad EL Fakir, Very well explained ! I don’t have any questions regarding this owesome guide but I just want to know what methodology you used to get a great grip on hybris ? do you have any tutorials or any content which can help others to put their efferots on the right track to get expertise as you have ?
Great article! thanks.
Hi,
Nice article!
Can you also please let us know how we can access S3 bucket folder via this
Config a base directory code :
bean id=”baseDirectoryTraining” class=”java.lang.String”
constructor-arg value=”#{baseDirectory}/${tenantId}/training”
The simple way is to use a custom job that connect to S3 bucket and get files then put them inside
#{baseDirectory}/${tenantId}/training
Otherwise you have to change a lot of things inside spring integration configuration which is not recommended due to performance issues that you will get during read/write on S3 Bucket.
Great post! One additional question, is there a way to use the hot folders to import predefined impexes? So just copy final impexes into the hotfolder and let the hotfolder import it automatically after each other?
Hello Sven, This isn’t an out-of-the-box feature However it’s possible to rearrange the component of the hotfolder (based on Spring Integration) to meet your need, Here is an example: 0) define a base directory <bean id="customBaseDirectory" class="java.lang.String"> <constructor-arg value="#{baseDirectory}/${tenantId}/impexes" /> </bean> 1) scan for impex files, update filename-regex <file:inbound-channel-adapter id="customBatchFiles" directory="#{customBaseDirectory}" filename-regex="^(.*)-(\d+)\.impex" comparator="fileOrderComparator"> <int:poller fixed-rate="1000" /> </file:inbound-channel-adapter> 2) move the file to processing and setup header <file:outbound-gateway request-channel="customBatchFiles" reply-channel="customBatchFilesProc" directory="#{customBaseDirectory}/processing" delete-source-files="true" /> 3) implement a new HeaderSetupTask to fill BatchHeader with the impex file <int:service-activator input-channel="customBatchFilesProc" output-channel="batchFilesHeaderInit" ref="customHeaderSetupTask" method="execute" /> <!-- should implement the CustomHeaderSetupTask based on the implementation of… Read more »
Thank you for your extensive reply. Also on behalf of Abdelilah I just heard 😉
Hi i need to get the count of the number of files getting processed in the hot folder implementation..Please help…
Very good post with clear explanation.
HI Mouad ,Is there any way we can change the poll time for certain hotfolder process say ex. orderstatus transaction poll.to 1 min poll interval
So is it possible to apply for certain process.not overall.as I see OOTB 1 sec polling is there
please specify how we can achieve the same .
Hi,
I want to upload Order and Order Entry from same file.. How to achieve this type of implementation.
Basically single csv file with multiple header
Thanks,
Vishal Patil
Hi Moud. I have question, suppose we want the polling time to change for a particular set of feed file say, order related,
So is it possible to customize. and what changes required for this.
Hi Mouad EL Fakir,
Could you help me with Media csv file , below is usecase
INSERT_UPDATE Media;mediaFormat(qualifier);code[unique=true] ;@media[translator=de.hybris.platform.impex.jalo.media.MediaDataTranslator][forceWrite=true];realfilename;mime[default=’image/jpeg’];$catalogVersion;folder(qualifier)[default=images]
;300Wx300H ;/300Wx300H/Image3.jpg ;$siteResource/300Wx300H/Image3.jpg ;Image3.jpg ; ; ;
And following is the error i’m getting:-
Missing value for 1: ;300Wx300H ;/300Wx300H/Image3.jpg ;$siteResource/300Wx300H/Image3.jpg ;Image3.jpg ; ; ;
Which all file formats used in cloud hot folder?
You are MAN thank you very much 🙂
Hello Mouad EL Fakir,
All CSVs placed in the “import” folder in the hotfolder file directory are copied to the “archive” folder and remain in the “archive” folder unless they are deleted.
The error-throwing CSVs need to be added to the “error” folder, but I cannot see the CSVs that throw some error in the “error” folder. What should be done to add the CSV to the “error” folder in case CSVs get errors.
Hey, Great Explanation!
What if there are many files but I want commerce to pick a file that starts with some specific pattern like files that start with “ABC”.
Hi,
Facing below issue after pasting the csv file. I followed the same steps as mentioned above.
ERROR [task-scheduler-9] [LoggingHandler] ErrorMessage [payload=org.springframework.messaging.MessageHandlingException: error occurred during processing message in ‘MethodInvokingMessageProcessor’ [org.springframework.integration.handler.MethodInvokingMessageProcessor@50169297]; nested exception is java.lang.NullPointerException, failedMessage=GenericMessage [payload=D:\CXCOM200500P_2-70004955\hybris\data\acceleratorservices\import\master\training\processing\unit-en-123456789.csv, headers={file_originalFile=D:\CXCOM200500P_2-70004955\hybris\data\acceleratorservices\import\master\training\unit-en-123456789.csv, id=7ac1c455-3d1d-0e53-656f-049067edfe02, file_name=unit-en-123456789.csv, file_relativePath=unit-en-123456789.csv, timestamp=1637083833508}], headers={id=bd509d8f-54c9-b18d-3992-c0d5be3dae57, timestamp=1637083833624}] for original GenericMessage [payload=D:\CXCOM200500P_2-70004955\hybris\data\acceleratorservices\import\master\training\unit-en-123456789.csv, headers={file_originalFile=D:\CXCOM200500P_2-70004955\hybris\data\acceleratorservices\import\master\training\unit-en-123456789.csv, id=9dff6ef2-86a1-55df-7a48-81653992e404, file_name=unit-en-123456789.csv, file_relativePath=unit-en-123456789.csv, timestamp=1637083833485}]
Hi
Did you resolve this error
If you resolve, could you please help me
Hi
Did you resolve this error ?
I need it
What is the fix for this?
I’m getting an error like this
File prefix unknown
getting this error: Missing value for 1
Could you please help me solve this
I am trying send mail to business with failed to load records Using Hot folder implementation.
Getting below error
ERROR [task-scheduler-5] [ErrorHandler] unexpected exception caught
org.springframework.messaging.MessagingException: Dispatcher failed to deliver Message; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available
at org.springframework.integration.dispatcher.AbstractDispatcher.wrapExceptionIfNecessary(AbstractDispatcher.java:133) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:120) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:425) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:375) ~[spring-integration-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
Could you please help me.
Mouad EL Fakir
Can you please write a page for CCV2 hotfloder configuration using Azure storage explorer
How can we change tenant ?
Hi Team, I have followed the same configuration for my project requirement. But records are not importing to Database and I am not getting any error at the time of server start up. Please find the below configuration for better understanding. Please suggest me, If a I did any wrong configuration. <?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:int=”http://www.springframework.org/schema/integration” xmlns:file=”http://www.springframework.org/schema/integration/file” xmlns:p=”http://www.springframework.org/schema/p” xmlns:context=”http://www.springframework.org/schema/context” xsi:schemaLocation=”http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd“> <bean id=”rackRoomBaseDirectory” class=”java.lang.String”> <constructor-arg value=”#{baseDirectory}/${tenantId}/rackroom” /> </bean> <file:inbound-channel-adapter id=”rackRoomEmployeeBatchFiles” directory=”rackRoomBaseDirectory” filename-regex=”^(.*)-(\d+)\.csv” comparator=”fileOrderComparator” > <int:poller fixed-rate=”1000″ /> </file:inbound-channel-adapter> <file:outbound-gateway request-channel=”rackRoomEmployeeBatchFiles” reply-channel=”rackRoomEmployeeBatchFilesProc” directory=”#{rackRoomBaseDirectory}/processing” delete-source-files=”true” /> <int:service-activator input-channel=”rackRoomEmployeeBatchFilesProc” output-channel=”batchFilesHeaderInit” ref=”rackRoomHeaderSetupTask” method=”execute” /> <bean… Read more »
Hello, I already have my hotfolders working “properly”, but I noticed some weird behaviour, I was wondering if you can help me to understand why.. This is my configuration (highLevel) myextensioncore-spring.xml 1)… hot-folder-productsA.xml 2)… hot-folder-productsB.xml hot-folder-productsA.xml: folder to scan(PATH): /opt/somepath/01_products filename-regex=”^(\d{3})_PRODUCT_INFO.csv” hot-folder-productsB.xml folder to scan(PATH): /opt/somepath/02_products filename-regex=”^(\d{3})_PRODUCT_INFO.csv” – If i send a file (01_PRODUCT_INFO.CSV) to the folder: 01_products, the product will be processed by the mappers-converters of productsA.xml – If i send a file (02_PRODUCT_INFO.CSV) to the folder: 02_products, the product will be processed by the mappers-converters of productsA.xml This is not the “proper behaviour” due this should be processed… Read more »
Hi ,
Can we use other file formats other than CSV