Integrate Java API in a Java project
In this section, you will learn how to integrate the Java API generated by AlchemyJ in a Java project.
Initializing ContextHolder
Import the generated package into your project.
Copy the libraries from AlchemyJ_Maven_Repository in the installation package to the local maven respository path.
The package needs to be initialized before using it. This can be done by calling the initByBasePackageName function.
AlchemyjClassContextHolder.getinstance().initByBasePackageName("myproject.stockorder");
- If DB connection is required, pass the data source to AlchemyjClassContextHolder by setDataSource.
AlchemyjClassContextHolder.getinstance().setDataSource(createDataSource());
Below is an example of how to initialize an AlchemyJ Java Package.
public static void init() throws Exception {
try {
AlchemyjClassContextHolder acch = AlchemyjClassContextHolder.getInstance();
WorkbookExecutorInitPropertyHolder initPropertyHolder = acch.getClassInitPropertyHolder();
initPropertyHolder.setStopOnFormulaError(true);
initPropertyHolder.setStopOnUserRaiseError(true);
initPropertyHolder.setThrowOnFormulaError(true);
initPropertyHolder.setThrowOnUserRaiseError(true);
initPropertyHolder.setApplicationPropertiesMap(createApplicationProperties());
/** Setup your datasource here. */
acch.setDataSource(createDataSource());
ObjectMapper objectMapper = new ObjectMapper();
// You can set your own time-zone here
objectMapper.setTimeZone(TimeZone.getDefault());
acch.setObjectMapper(objectMapper);
acch.initByBasePackageName("myproject.stockorder");
} catch (Exception e) {
log.error("Could not initialize AlchemyJ class context holder.", e);
throw e;
}
}
Create Data source
private static DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("[DB Driver Clasee]");
dataSource.setJdbcUrl("[DB Connection String]");
dataSource.setUser("[User name]");
dataSource.setPassword("[Password]");
dataSource.setMaxPoolSize(3);
return dataSource;
}
Setting Pool Size
private Map<String,Integer> createPoolSizeMap(){
Map<String,Integer> map = new HashMap<>();
map.put("com.axisoft.alchemyj.sample.CusomterObj",10);
return map;
}
public void init(){
AlchemyjClassContextHolder.getInstance()
.getClassInitPropertyHolder().setAlchemyjClassPoolLimitMapping(createPoolSizeMap());
}
Destroy ContextHolder
AlchemyjClassContextHolder.getInstance().destroy();
Setting Application Properties
private Map<String, String> createApplicationProperties() {
Map<String, String> result = new LinkedHashMap<>();
result.put("Property_name_1", "192.168.5.149");
result.put("Property_name_2", "25.0");
result.put("Property_name_3", "sample@gmail.com");
return result;
}
public void init(){
AlchemyjClassContextHolder.getInstance()
.getClassInitPropertyHolder().setApplicationPropertiesMap(createApplicationProperties());
}
Getting an object instance
Below is an example of how to get and close an object instance.
Create an object instance just like creating a normal java object from a java class:
CustomerObject customer = new CustomerObject();
After using the object, it should be closed.
CustomerObject customer = new CustomerObject();
....(Your logic code)
customer.close(); //to release the resources
Available ClassContextHolder Properties
// To get the property holder instance to save class initialization properties.
public WorkbookExecutorInitPropertyHolder getClassInitPropertyHolder()
// Apply your database DataSource object to the current AlchemjJ project, which will provide database connections.
public void setDataSource(DataSource dataSource)
// Apply object converter to convert object to string, string to object, it must contain time-zone information.
public void setObjectMapper(ObjectMapper objectMapper)
// Before creating a new object from the generated classes, you must call this method first to initialize them all,
// the package names should cover all generated classes you will use in your project.
public void initByBasePackageName(String... packageNames)
Methods of WorkbookExecutorInitPropertyHolder
public void setStopOnFormulaError(boolean stopOnFormulaError)
- true: If there is native formula calculation error, stop and do not calculate other cells. The calculation order is dynamic.
- false: Ignore all formula calculation error, set error code back to the cell if error and continue to calculate all other cells.
- default: false
public void setStopOnUserRaiseError(boolean stopOnUserRaiseError)
- true: If there is error raised by the formula ajRaiseError(), stop and do not calculate other cells. The calculation order is dynamic.
- false: Ignore all errors from ajRaiseError(), set error code back to the cell, and continue to calculate all other cells.
- default: true
public void setThrowOnFormulaError(boolean throwOnFormulaError)
- true: If there is native formula calculation error, throw a Java Exception in generated Java code.
- false: If there is native formula calculation error, DO NOT throw a Java Exception in generated Java code.
- default: true
public void setThrowOnUserRaiseError(boolean throwOnUserRaiseError)
- true: If there is error raised by formula: ajRaiseError(), throw a Java Exception in generated Java code.
- false: Ignore all errors from ajRaiseError(), DO NOT throw a Java Exception in generated Java code.
- default: true
public void setApplicationPropertiesMap(Map<String,String> propertyMap)
- key: Should be the same as the value in column 'Name' in sheet '%%AppConfig'
- value: Property value, when a workbook is initialized, the value will be applied into cells corresponding to the 'key' cell, it may affect your formula result if the formula contains the cell which current value locates.
public void setLicenseFileLocation(String path) Specify the absolute path of your license file.
public void setAlchemyjClassPoolLimitMapping(Map<String,Integer> alchemyjClassPoolLimitMapping)
- key: Full class name, e.g. com.axisoft.alchemyj.sample.CusomterObj
- value: Number of the cached workbook for the current class specified by the string of the key.