jms在websphere集群中发布/订阅

时间:2013-11-09 13:31:34

标签: jms websphere java-ee-6 publish-subscribe message-driven-bean

我编写了一个发布/订阅者示例,并将其部署在集群环境中的websphere应用服务器上。 但是当我订阅消息时,MDB只读取了每条消息,只读了一次。 我在websphere和MDB中配置了持久订阅,我还将Share durable subscriptions设置为always shared并设置Always activate MDBs in all servers。 每条消息只被阅读一次,我认为它消耗或其他东西。 我在MDB中设置@ActivationConfigProperty(propertyName = "useSharedSubscriptionInClusteredContainer",propertyValue = "false")(基于http://docs.oracle.com/cd/E18930_01/html/821-2438/gjzpg.html#MQAGgjzpg),但没有任何反应。 我无法在所有服务器中订阅消息。 我还在websphere总线中将messaging engine policy设置为High availability。 使用Default messaging provider

问题在哪里?

这是我的出版商

@WebServlet("/publishServlet")
public class Testpublish extends HttpServlet {

    @Resource(mappedName = "jms/ConnFact")
    private static TopicConnectionFactory topicConnectionFactory;

    @Resource(mappedName = "jms/topicJ")
    private static Topic topic;

    TopicConnection connection = null;
    TopicSession session = null;
    TopicPublisher publisher = null;
    TextMessage message = null;
    final int NUM_MSGS = 5;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        ServletOutputStream out = response.getOutputStream();
        out.println("Start Testing");
        System.out.println("Start Testing");

        try {
            connection = topicConnectionFactory.createTopicConnection();
            session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            publisher = session.createPublisher(topic);
            message = session.createTextMessage();

            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText("This is testMessage " + (i + 1));
                System.out.println("Sending testMessage: " + message.getText());
                out.println("Sending testMessage: " + message.getText());
                publisher.publish(message);
            }

            connection.close();
            out.println("Finish Testing");
            System.out.println("Finish Testing");

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

和我的订阅者

@MessageDriven(mappedName = "jms/topicJ", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "subscriptionDurability",propertyValue = "Durable"),
        @ActivationConfigProperty(propertyName = "clientId",propertyValue = "MyID"),
        @ActivationConfigProperty(propertyName = "subscriptionName",propertyValue = "MySub")
    })

public class testsubscribe implements MessageListener {

    @Override
    public void onMessage(Message message) {
        TextMessage txtMessage = (TextMessage) message;
        try {
            System.out.println("---------MESSAGE RECIEVED------------" + txtMessage.getText()
                    + " ..............");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:2)

我通过禁用websphere总线中的messaging engine policy解决了这个问题。现在效果很好。