Thanks Marcel, thats really useful to know. The last test thats been worrying
me somewhat has been trying to get multiple threads to add a file to the
same workspace. Each thread gets its own session in its run() method,
creates a nt:folder and adds a nt:file and nt:resource. One thread succeeds
but all others fail with an ItemNotFoundException. The output is as follows: 

thread 1 added in 1343ms, logging out of session 
javax.jcr.ItemNotFoundException:
5cc05aef-5ad0-434a-9b47-4d7e3d3ba7be/{http://www.jcp.org/jcr/1.0}data 
at
org.apache.jackrabbit.core.ItemManager.createItemInstance(ItemManager.java:465) 
at org.apache.jackrabbit.core.ItemManager.getItem(ItemManager.java:321) 
at
org.apache.jackrabbit.core.ItemImpl.restoreTransientItems(ItemImpl.java:709) 
at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:1209) 
at org.apache.jackrabbit.core.SessionImpl.save(SessionImpl.java:806) 
at com.axxia.test.JackrabbitThreadTests.run(JackrabbitThreadTests.java:102) 
thread 0 caught repositoryexception in run():
javax.jcr.ItemNotFoundException:
5cc05aef-5ad0-434a-9b47-4d7e3d3ba7be/{http://www.jcp.org/jcr/1.0}data 
thread 0 added in -1144342173968ms, logging out of session 


This is the code I used:

package com.axxia.test;
import junit.framework.TestCase;
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.config.RepositoryConfig;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Date;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.RowIterator;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.Repository;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;
import javax.jcr.Workspace;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.RowIterator;
import javax.jcr.RepositoryException;

import java.util.Calendar;

public class JackrabbitThreadTests extends Thread 
{

        private static final String DMS_DIR = "C:/DocumentSearchTests/dms";
        private static final String DMS_CONFIG =
"C:/DocumentSearchTests/config/repository.xml";
        private static final String TEST_DIR =
"C:/DocumentSearchTests/testdocuments/word/";
        private static final String MIME_TYPE = "application/msword";
        
        
        
        private String name;
        private String testFile;
        private static Repository repository;

        public static void main(String[] args) throws Exception 
        {
                setUp();
                int numberOfThreads = 2;
                for (int i = 0; i < numberOfThreads; i++)
                {
                        //new JackrabbitThreadTests("thread" + i, 
"TIJ2.doc").start();
                        new JackrabbitThreadTests("thread" + i, 
"testwordfile.doc").start();
                }
        }
        
        public JackrabbitThreadTests(String name, String testFile)
        {
                this.name = name;
                this.testFile = testFile;
        }
        
        public static void setUp() throws Exception
        {       
                System.out.println("First clean dms directory");
                boolean deleted = deleteDirectory(new File(DMS_DIR));
                System.out.println("Deleted: " + deleted);
                //Recreate index directory
                new File(DMS_DIR).mkdir();
                //Create JR repository
                //Now create dms repository and get the root node
                InputStream inputStream = new 
File(DMS_CONFIG).toURL().openStream();
                RepositoryConfig config = RepositoryConfig.create(inputStream, 
DMS_DIR);
                inputStream.close();
            repository = RepositoryImpl.create(config);
        }
        
        public void run()
        {
                System.out.println(name + " started");
            SimpleCredentials credentials = new SimpleCredentials("username",
"password".toCharArray());
            Session session = null;
            Node root = null;
            long t1 = 0;
            long t2 = 0;
            try
            {
                    session = repository.login(credentials);
                    root = session.getRootNode();
                File file = new File(TEST_DIR + testFile);
                FileInputStream fis = new FileInputStream(file);
                Node folderNode = root.addNode(name + "folder", "nt:folder");
                Node fileNode = folderNode.addNode(file.getName(), "nt:file");
                    Node resourceNode = fileNode.addNode("jcr:content", 
"nt:resource");
                    resourceNode.setProperty("jcr:mimeType", MIME_TYPE);
                    resourceNode.setProperty("jcr:data", fis);
                    fis.close();
                        Calendar lastModified = Calendar.getInstance();
                        lastModified.setTimeInMillis(file.lastModified());
                    resourceNode.setProperty("jcr:lastModified", lastModified); 
 
                    //Add the file and time
                    t1 = new Date().getTime();
                    //System.out.println(name + " before save");
                    session.save(); 
                    //System.out.println(name + " after save");
                    t2 = new Date().getTime();
                    
                    //Now look inspect the repository
                    //Workspace workspace = session.getWorkspace();
                        //printWorkspaceContents(workspace);
            }
            catch (IOException ioe)
            {
                System.out.println(name + " caught IOException in run(): " +
ioe.getMessage());;
            }
            catch (RepositoryException e)
            {
                System.out.println(name + " caught RepositoryException in 
run(): " +
e.toString());
                e.printStackTrace();
                
            }
            System.out.println(name + " added file in " + (t2 - t1) + "ms, 
logging
out of session");
            if (session != null)
                {
                        session.logout();
                }
        }
        
        
        private static boolean deleteDirectory(File path) 
        {
                if (path.exists()) 
                {
                    File[] files = path.listFiles();
                    for(int i=0; i<files.length; i++) {
                        if(files[i].isDirectory()) {
                            deleteDirectory(files[i]);
                        }
                        else 
                        {
                            files[i].delete();
                        }
                    }
                }
                return(path.delete());
        }

        
        private void printWorkspaceContents(Workspace workspace) throws
RepositoryException
        {
                QueryManager queryManager = workspace.getQueryManager();
        Query query = queryManager.createQuery("//*", Query.XPATH);
        QueryResult result = query.execute();
        printResult(result);
        }
        private void printResult(QueryResult result) throws RepositoryException
        {
        RowIterator iter = result.getRows();
        if (iter.hasNext())
        {       
                System.out.println("One or more hits found");
                NodeIterator it = result.getNodes();
                while (it.hasNext()) {
                        Node n = it.nextNode();
                        System.out.println(n.getName() + ": " + n.getPath());
                }
        }
        else
        {
                System.out.println("No hits found");
        }               
        }       
}


Sorry to pass you this one too but we are on a fairly tight schedule to get
work started on our dms component. Thanks for all the help, its cleared up a
lot of issues.
Thomas

--
View this message in context: 
http://www.nabble.com/Is-doc-addition-indexing-synchronous-or-asynchronous--t1400122.html#a3842414
Sent from the Jackrabbit - Dev forum at Nabble.com.

Reply via email to