Glassfish 4

时间:2016-10-14 21:59:29

标签: java glassfish jms java-ee-7

我最近为Java EE JMS API编写了一些测试代码,偶然发现了一些似乎是我的错误。

测试非常简单:CDI bean连接到JSF页面,并将预定义的JMSMessage发送到Queue。有一个消息驱动的bean正在监听Queue上的消息,它会在其onMessage方法中回复传入的消息。 通过来自JSF页面的另一个调用,CDI bean尝试同步接收新消息并将它们显示到前端。

以下是来源:

MDB

@JMSDestinationDefinition(
    name = "java:app/jms/SomeQueue",
    interfaceName = "javax.jms.Queue",
    destinationName = "SomeQueue")
@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "java:app/jms/SomeQueue"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MyBean implements MessageListener {

@Inject
private JMSContext context;
@Resource
private MessageDrivenContext mdc;

@Resource(lookup = "java:app/jms/SomeQueue")
private Queue queue;

@Override
public void onMessage(Message message) {
    TextMessage tMessage = (TextMessage) message;
    try {
        context.createProducer().send(queue, tMessage.getText()+", Answer");
    } catch (JMSException e) {
        mdc.setRollbackOnly();
        e.printStackTrace();
    }
}
}

CDI bean

@RequestScoped
@Named("jmssender")
public class JMSSender {

@Inject
private JMSContext context;

@Resource(lookup = "java:app/jms/SomeQueue")
private Queue queue;

@Inject
private UserTransaction utx;

private String answer;

private TextMessage message;


private int num;

public void sendMessage() {
    try {
        utx.begin();
        message = context.createTextMessage();
        String message = "This is message Nr. " + num;
        try {
            this.message.setText(message);
        } catch (JMSException e) {
            e.printStackTrace();
            message = "An error occurred";
        }
        context.createProducer().send(queue, message);
        utx.commit();
    } catch (HeuristicRollbackException e) {
        e.printStackTrace();
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        e.printStackTrace();
    } catch (NotSupportedException e) {
        e.printStackTrace();
    }
}

public void receive() {
    try {
        utx.begin();
        answer = context.createConsumer(queue).receiveBody(String.class);
        utx.commit();
    } catch (NotSupportedException |
            SystemException |
            HeuristicRollbackException |
            RollbackException |
            HeuristicMixedException e) {
        answer = "Error receiving message";
        e.printStackTrace();
    }
}
       ------getters and setters-----

num属性用于标识每封邮件的发送。我已使用message属性检查我的JMSMessages是否正确发送。

问题

发送消息后,我尝试收到答案并在我的前端看到它,它看起来像这样:

这是消息Nr。 8 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0 Ve版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0 2.0版Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 2.0版Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0 Versio n 2.0版本2.0 2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版2.0版Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 2.0版Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2.0版本2。 0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version 2.0 Version

显然它将我所包含的字符串部分循环到原始消息中,但是当更改代码中的exaclty时,附加字符串",Answer"被添加到消息中,消息本身"这是消息NR。 X"循环。此外,似乎循环片段的长度取决于按下"发送"之间的间隔有多大。并且"接收"按钮是。

如果我只指定一个字符串,如" Hello"或者只是使用tMessage.getText()发回消息,它工作正常并且不会循环任何特定部分

我犯了错误还是实际上是个错误?

更新

- 删除UserTransaction接口的所有用法(认为可能需要确认CDI Bean中的消息) - 没有帮助

0 个答案:

没有答案