如何配置FileEntityStoreService?激活时遇除零异常问询
java.lang.ArithmeticException: / by zero in Apache Polygene FileEntityStore Got it, let's tackle this issue head-on. The root problem here is that the slices configuration property for FileEntityStoreService isn't being applied correctly—your assumption about a default value of 1 is spot-on, but in your current setup, that default isn't kicking in, leading to a division-by-zero when the store tries to calculate entity file paths.
Here are two straightforward, reliable ways to fix this:
1. Explicitly Set Defaults in Your Assembly Code
You can define the default slices value directly when assembling the FileEntityStoreAssembler. Modify your code to add a withDefaults block that explicitly sets slices to 1:
import org.apache.polygene.api.common.Visibility; import org.apache.polygene.api.structure.Application; import org.apache.polygene.bootstrap.Energy4Java; import org.apache.polygene.entitystore.file.assembly.FileEntityStoreAssembler; import org.apache.polygene.index.rdf.assembly.RdfNativeSesameStoreAssembler; import org.apache.polygene.library.fileconfig.FileConfigurationAssembler; public class FileStoreException { public static void main(String[] args) throws Exception { Energy4Java polygene = new Energy4Java(); Application application = polygene.newApplication(factory -> factory.newApplicationAssembly( module -> { new FileConfigurationAssembler() .visibleIn(Visibility.application) .assemble(module); // Add this withDefaults block to set the slices default new FileEntityStoreAssembler() .withConfig(module, Visibility.application) .withDefaults(properties -> properties.set("slices", 1)) // <-- Fix right here .assemble(module); new RdfNativeSesameStoreAssembler() .withConfig(module, Visibility.application) .assemble(module); module.defaultServices(); }) ); application.activate(); } }
This guarantees that even if no external configuration is present, the slices value will default to 1, eliminating the division-by-zero error during activation.
2. Use a Configuration Properties File
Since you're already using FileConfigurationAssembler, you can externalize the setting with a properties file. Create a file named filestore.properties (place it in your classpath root, or configure the file assembler to point to its path) with this line:
org.apache.polygene.entitystore.file.slices=1
This approach is ideal if you want to keep configuration separate from your codebase, making it easier to adjust values later without recompiling.
Why This Fixes the Issue
Looking at your stack trace, the error occurs in FileEntityStoreMixin.getDataFile() where the code divides by the slices value to map entities to their storage files. By setting slices to a positive integer (1 is the intended default), you avoid the invalid division-by-zero scenario. The default value should be applied automatically, but in your setup, the config initialization wasn't picking it up—explicitly setting it closes that gap.
内容的提问来源于stack exchange,提问作者Nina

