并发访问@Lock(LockType.WRITE)方法 [英] Concurrent access to a @Lock(LockType.WRITE) method

查看:252
本文介绍了并发访问@Lock(LockType.WRITE)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完成什么:

我有一个JMS队列,我想异步地从该队列中获取元素并发送到打印机。这个打印机是非常具体和笨的,所以它不能排队在它或缓冲任何东西。我必须在本打印机上按顺序执行5个步骤,排队的电子元件的步骤不能与另一个元素的步骤重叠。对于每个步骤,打印机将在成功时返回1,而在失败时打印机将返回另一个数字。

I have a JMS queue and I want to asynchronously get elements from that queue and send to a Printer. This printer is very specific and dumb so it can't queue on it's on or buffer anything at all. I must perform 5 steps in sequence on this printer and the steps of a queued elemet must not overlap the steps of another element. For each step the printer will return 1 on success and another number on failure.

当前的实现是一个具有 onMessage 方法调用的MDB来自Singleton ejb的@Lock(LockType.WRITE)方法。

The current implementation is an MDB with an onMessage method call to a @Lock(LockType.WRITE) method from a Singleton ejb.

MDB:

    @MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationLookup",
        propertyValue = "jms/MyQueue"),
@ActivationConfigProperty(propertyName = "destinationType",
        propertyValue = "javax.jms.Queue")})

public class SimpleMessageBean implements MessageListener {

@Resource
private MessageDrivenContext mdc;
static final Logger logger = Logger.getLogger("SimpleMessageBean");

@EJB
private Print print;

public SimpleMessageBean() {
}

@Override
public void  onMessage(Message inMessage) {
    try {
        if (inMessage instanceof TextMessage) {
            logger.log(Level.INFO, "MESSAGE BEAN: Message received: {0}", inMessage.getBody(String.class));

            String params[] = ((TextMessage) inMessage).getText().split("\\s+");
            print.doStuff(params[0], params[1]);
        } else {
            logger.log(Level.WARNING,"Error")
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Rollback Happening: {0}", e.toString());
        mdc.setRollbackOnly();
    }
}

单身人士:

@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Startup
@Singleton
@AccessTimeout(value = 120000)
public class Print {

static final Logger logger = Logger.getLogger("SimpleMessageBean");

public Print() {
}

@Lock(LockType.WRITE)
public void doStuff(String cpf, String value) throws Exception{
        try{
            int return;
            return = Bematech.AbreCupom(cpf);
            System.out.println("Stage0 = " + return);

            return = Bematech.VendeItem("111112", "Parking", "FF", "hours", "1", 2, value, "%", "00,00");
            System.out.println("Stage1 = " + return);

            return = Bematech.IniciaFechamentoCupom("D", "%", "00,00");
            System.out.println("Stage2 = " + return);

            return = Bematech.EfetuaFormaPagamento("Dinheiro", value);
            System.out.println("Stage3 = " + return);

            return = Bematech.TerminaFechamentoCupom("Thankyou ofr your support");
            System.out.println("Stage4 = " + return);


        }catch ( Exception MensagemErro ){
            throw MensagemErro;
        }
}

我预计这个实现从队列中接收数据并在doStuff()方法被锁定的同时打印一个元素,AKA:

I expected this implementation to receive the data from the queue and print it one element at a time as the doStuff() method is locked, AKA:

Stage0=1
Stage1=1
Stage2=1
Stage3=1
Stage4=1
Stage0=1
Stage1=1
Stage2=1
Stage3=1
Stage4=1
Stage0=1
...

但是有时候(似乎是随机的,但是当几个消息一次排队时它会更频繁地发生)我得到两个并发访问打印方法:

But sometimes (seems to be random but it happens more often when several messages are queued at once) i get two concurrent access to the print method:

Stage0=1
Stage1=1
Stage0=1
Stage2=1
Stage1=1
Stage3=1
Stage2=1
...

从什么是打印我可以看到2个元素重叠在打印机访问。

From what is being print I can see that 2 elements are overlapping in the printer access.

有用的信息:

Java EE 7.
Glassfish 4-0 。
不使用JBOSS。

Java EE 7. Glassfish 4-0. Not Using JBOSS.

任何想法为什么会发生这种情况?

Any ideas why this is happening?

推荐答案

同步关键字解决了我的问题。没有找到原始代码出错了。

Synchronized keyword solved my problem. Did not find out what was wrong with original code.

这篇关于并发访问@Lock(LockType.WRITE)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆