繁体   English   中英

tomcat启动时OverlappingFileLockException

[英]OverlappingFileLockException in tomcat start-up

我在我的tomcat中添加了自定义jndi-resource工厂 (jndi-resource用于嵌入式hornetq的ConnectionFactory)。 我的资源需要一些配置文件; 我把它们放在${catalina_home}/hornetq文件夹中。 我有一个测试网络应用程序在tomcat启动时使用此资源。 在tomcat启动时,当test-web-app想要使用我的资源时,资源想要锁定配置文件但它不能,并抛出`OverlappingFileLockException:

java.nio.channels.OverlappingFileLockException
    at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
    at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
    at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1056)
    at org.hornetq.core.server.impl.FileLockNodeManager.tryLock(FileLockNodeManager.java:266)
    at org.hornetq.core.server.impl.FileLockNodeManager.isBackupLive(FileLockNodeManager.java:82)
    at org.hornetq.core.server.impl.HornetQServerImpl$SharedStoreLiveActivation.run(HornetQServerImpl.java:2161)
    at org.hornetq.core.server.impl.HornetQServerImpl.start(HornetQServerImpl.java:450)
    at org.hornetq.jms.server.impl.JMSServerManagerImpl.start(JMSServerManagerImpl.java:485)
    at org.hornetq.jms.server.embedded.EmbeddedJMS.start(EmbeddedJMS.java:115)
    at ...

是否可以在tomcat或OS中禁用文件锁定( ${catalina_home}/hornetq目录中的${catalina_home}/hornetq )?

更新:

我在tomcat中的context.xml文件中的jndi-resource(一个名为: /ConnectionFactory连接工厂在hornetq-jms.xml定义):

<Resource name="jms/ConnectionFactory" auth="Container" 
    type="javax.jms.ConnectionFactory"
    factory="com.wise.jms.hornetq.embedded.HornetqConnectionFactoryBuilder"
    cf-name="/ConnectionFactory" singletone="true"/>

我的工厂类: HornetqConnectionFactoryBuilder 我将包含此类的jar放在${catalina_home}/lib目录中(嵌入式hornetq将在第一个getObjectInstance方法调用中启动):

public class HornetqConnectionFactoryBuilder implements ObjectFactory{

    private EmbeddedJMS embeddedJMS;
    private static final String ConnectionFactoryName = "cf-name";
    private static final String HornetqConfigDirectoryPath = getCatalinaHomePath() + "/conf/hornetq/";
    private static final String JmsConfigFilePath = HornetqConfigDirectoryPath + "hornetq-jms.xml";
    private static final String HornetqConfigFilePath = HornetqConfigDirectoryPath + "hornetq-configuration.xml";

    @Override
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception{
        Properties properties = initProperties((Reference) obj);
        validateProperties(properties);
        initHornetq();
        String connectionFactoryJndiName = (String) properties.get(ConnectionFactoryName);
        return embeddedJMS.lookup(connectionFactoryJndiName);
    }

    private synchronized void initHornetq() throws Exception{
        if (embeddedJMS == null){
            embeddedJMS = new EmbeddedJMS();
            embeddedJMS.setJmsConfigResourcePath(JmsConfigFilePath);
            embeddedJMS.setConfigResourcePath(HornetqConfigFilePath);
            embeddedJMS.start();
        }
    }

    private Properties initProperties(Reference reference) throws IOException{
        Enumeration<RefAddr> addresses = reference.getAll();
        Properties properties = new Properties();
        while (addresses.hasMoreElements()) {
            RefAddr address = addresses.nextElement();
            String type = address.getType();
            String value = (String) address.getContent();
            properties.put(type, value);
        }
        return properties;
    }

    private void validateProperties(Properties properties){
        validateSingleProperty(properties, ConnectionFactoryName);
    }

    private static String getCatalinaHomePath(){
        String catalinaHome = System.getenv("CATALINA_HOME");
        if (catalinaHome == null){
            throw new IllegalArgumentException("CATALINA_HOME environment variable should be set");
        }
        return "file:///" + catalinaHome.replaceAll("\\\\", "/");
    }

    private static void validateSingleProperty(Properties properties, String propertyName){
        if (!properties.containsKey(propertyName)){
            throw new IllegalArgumentException(propertyName + " property should be set.");
        }
    }
}

我的测试网络应用程序Test类(测试方法将在test-web-app启动时午餐):

public class Test{
    private ConnectionFactory connectionFactory;

    public Test() throws NamingException{
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        connectionFactory = (ConnectionFactory) envCtx.lookup("jms/ConnectionFactory");
    }

    public void test() throws JMSException{
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(true,Session.SESSION_TRANSACTED);
        Queue queue = session.createQueue("testQueue");
        MessageProducer messageProducer = session.createProducer(queue);
        MessageConsumer messageConsumer = session.createConsumer(queue);
        TextMessage message1 = session.createTextMessage("This is a text message1");
        messageProducer.send(message1);
        session.commit();
        TextMessage receivedMessage = (TextMessage) messageConsumer.receive(5000);
        session.commit();
        System.out.println("Message1 received after receive commit: " + receivedMessage.getText());
    }

    public void setConnectionFactory(ConnectionFactory connectionFactory){
        this.connectionFactory = connectionFactory;
    }
}

注意:当我把(hornetq)配置文件放在jar类路径中时,我没有问题,一切都很好!! (tomcat:6,hornetq:2.4.0.Final,OS:windows 7)

你在Tomcat中有其他部署吗? 很有可能一旦你重新启动tomcat,首先会部署一些其他项目使用lib文件夹中的文件。

尝试在新的tomcat安装中部署它,或从Tomcat的webapps文件夹中删除其他文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM