A few more snippets of code that may be helpful:
public String storeContent(String fileName, InputStream inputStream,
String contentType, String contentEncoding) throws JcrServiceException {
String documentId;
Session session = null;
try {
session = getJcrSession();
... store the content.
}
private Session getJcrSession() throws ClassNotFoundException,
RepositoryException {
Repository repository = repositoryProvider.getRepository();
// creates a default workspace
Session session = repository.login(new
SimpleCredentials(jcrUsername, jcrPassword.toCharArray()));
if (logger.isDebugEnabled())
logger.debug("Content Repository User: " +
session.getUserID());
Workspace workspace = session.getWorkspace();
if (logger.isDebugEnabled())
logger.debug("Content Repository Workspace: " +
workspace.getName());
try {
workspace.getNamespaceRegistry().registerNamespace(NAMESPACE_PREFIX,
NAMESPACE_URI);
} catch (NamespaceException e) {
logger.debug("The namespace has already been registered.");
// This is OK.
} catch (RepositoryException e) {
throw new RepositoryException("Could not create JCR
session.", e);
}
return session;
}
On 2015-11-13 14:33, [email protected] wrote:
Hi,
I am new to JackRabbit and using version 2.11.2. I am using
JackRabbit to store documents in a multi-threaded environment. I
noticed that the time it takes to retrieve the root node is
inconsistent and slow (several seconds +) and degrades over time
(after 50K plus child nodes retrieval is taking ~15 seconds).
Originally, I was using code as follows to obtain a repository:
public Repository getRepository() throws ClassNotFoundException,
RepositoryException {
ServiceLoader.load(Class.forName("org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory"));
return JcrUtils.getRepository(jackabbitServerUrl);
}
Then I came across the following thread:
http://jackrabbit.510166.n4.nabble.com/getRootNode-takes-27-seconds-td1571027.html#a1571302
This thread had some useful information (BatchReadConfig), but I am
not certain how to use the API to take advantage of it. I have
changed my code to the following but it doesn't appear that node
retrieval performance has improved, is there something I am
missing/doing wrong?
1) Repository Factory
public Repository getRepository(@SuppressWarnings("rawtypes") Map
parameters) throws RepositoryException {
String repositoryFactoryName = parameters != null && (
parameters.containsKey(PARAM_REPOSITORY_SERVICE_FACTORY) ||
parameters.containsKey(PARAM_REPOSITORY_CONFIG))
?
"org.apache.jackrabbit.jcr2spi.Jcr2spiRepositoryFactory"
: "org.apache.jackrabbit.core.RepositoryFactoryImpl";
Object repositoryFactory;
try {
Class<?> repositoryFactoryClass =
Class.forName(repositoryFactoryName, true,
Thread.currentThread().getContextClassLoader());
repositoryFactory = repositoryFactoryClass.newInstance();
}
catch (Exception e) {
throw new RepositoryException(e);
}
if (repositoryFactory instanceof RepositoryFactory) {
return ((RepositoryFactory)
repositoryFactory).getRepository(parameters);
}
else {
throw new RepositoryException(repositoryFactory + " is not
a RepositoryFactory");
}
}
2) Use the factory to get a repo:
public Repository getRepository() throws ClassNotFoundException,
RepositoryException {
Map<String, RepositoryConfig> parameters =
Collections.singletonMap(
"org.apache.jackrabbit.jcr2spi.RepositoryConfig",
(RepositoryConfig) new
RepositoryConfigImpl(jackabbitServerUrl));
return getRepository(parameters);
}
3) Repository Config:
private static final class RepositoryConfigImpl implements
RepositoryConfig {
private String jackabbitServerUrl;
private RepositoryConfigImpl(String jackabbitServerUrl) {
super();
this.jackabbitServerUrl = jackabbitServerUrl;
}
public CacheBehaviour getCacheBehaviour() {
return CacheBehaviour.INVALIDATE;
}
public int getItemCacheSize() {
return 100;
}
public int getPollTimeout() {
return 5000;
}
public RepositoryService getRepositoryService() throws
RepositoryException {
BatchReadConfig brc = new BatchReadConfig() {
public int getDepth(Path path, PathResolver resolver)
throws NamespaceException {
return 1;
}
};
return new RepositoryServiceImpl(jackabbitServerUrl, brc);
}
}
Thanks for your time.
David