声纳违规:狡猾 - 从实例方法写入静态字段

时间:2015-09-09 10:17:47

标签: java

我有一个变量 - “protected static Context jndi;”在我的班级中,“Context”是一个界面。当我尝试在下面提到的方法中访问它时,它会生成标题中提到的声纳违规

public JMSQueueResource createQueueResource(String queueBindingName, String qcfBindingName, boolean messagePersisted, boolean autoAcknowledge, boolean nonJMS) throws JMSException, NamingException {
    JMSQueueResource qResource = new JMSQueueResource();

    try {
        jndi = createInitialContext();
        if (queueConnectionFactory == null) {
            queueConnectionFactory = (QueueConnectionFactory) lookup(jndi, qcfBindingName);
        }
        qResource.theQueueConnection = queueConnectionFactory.createQueueConnection();

        if (autoAcknowledge) {
            qResource.theQueueSession = qResource.theQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        }
        else {
            qResource.theQueueSession = qResource.theQueueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
        }
        Queue queue = (Queue) lookup(jndi, queueBindingName);

        //if (nonJMS && queue instanceof com.ibm.mq.jms.MQQueue) {
        //    com.ibm.mq.jms.MQQueue q = (com.ibm.mq.jms.MQQueue) queue;
        //    q.setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);
        //}

        qResource.theQueueSender = qResource.theQueueSession.createSender(queue);
        if (messagePersisted) {
            qResource.theQueueSender.setDeliveryMode(DeliveryMode.PERSISTENT);
        }
        else {
            qResource.theQueueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        qResource.theQueueConnection.start();
    } 
    catch (JMSException jmse) {
        throw jmse;
    } 
    catch (NamingException ne) {
        throw ne;
    } 
    finally {
        if(jndi != null){
            jndi.close();
        }
    }

    return qResource;
}

我可以看到有人建议使用Atomic Integer包装器。解决这个问题的最佳方法是什么?

0 个答案:

没有答案