JMS - 在WebSphere Application Server 7上使用主题

时间:2015-10-10 09:21:20

标签: websphere-7 jms-topic

我有一个特定主题的发送者和接收者。我在WAS 7.0上运行发送器和接收器作为servlet。 正在WAS上设置TopicTopic Connection Factory。但是我无法收到发送的消息。当我尝试使用Queue而不是Topic时,它工作正常。

以下是我使用的代码。

public class CommonServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;
    private static boolean initialized = false;
    private static InitialContext initialContext = null;
    private ConnectionFactory connectionFactory = null;
    private Destination destination = null;

    protected final void initialize(){
        try{
            if( initialized == false ){
                // Get JNDI initial context
                initialContext = new InitialContext();

                // Flag as initialized
                initialized = true;
            }
        }
        catch( Throwable t ){
            System.out.println( t.getMessage() );
        }
    }

    /**
     * @return
     */
    protected final Destination getDestination(){
        if( destination != null ){
            return destination;
        }

        try{
            destination = (Destination) initialContext.lookup("jms/TestTopic" );
        }
        catch( NamingException e ){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return destination;
    }

    /**
     * @return
     */
    protected final ConnectionFactory getConnectionFactory(){
        try{
            connectionFactory = (ConnectionFactory) initialContext.lookup("jms/TestTopicCF" );
        }
        catch( NamingException e ){
            e.printStackTrace();
        }
        return connectionFactory;
    }
}

发件人servlet类

public class Sender extends CommonServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException{
        System.out.println("inside do get of sender");
        doPost( req, resp );
    }

    @Override
    protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException{
        String message = req.getParameter( "message" );
        sendMessage( message );
    }

    private void sendMessage( String messageText ){
        initialize();
        try{
            ConnectionFactory factory = getConnectionFactory();

            Destination destination = getDestination();

            Connection connection = factory.createConnection();
            connection.start();

            Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE );

            MessageProducer sender = session.createProducer( destination );

            TextMessage txtMsg = session.createTextMessage( messageText );

            sender.send( txtMsg );
        }
        catch( Exception ex ){
            ex.printStackTrace();
        }
    }
}

Receiver servlet类

public class Receiver extends CommonServlet{

    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException{
        doPost( req, resp );
    }

    @Override
    protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException{
        getMessage();
    }

    private void getMessage(){
        initialize();
        ConnectionFactory factory = getConnectionFactory();

        Destination destination = getDestination();

        try{
            Connection connection = factory.createConnection();

            connection.start();

            Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE );

            MessageConsumer receiver = session.createConsumer( destination );

            Message message = receiver.receive( 4000 );

            System.out.println( message );//COMING AS NULL
        }
        catch( JMSException e ){
            e.printStackTrace();
        }

    }

}

在{strong>资源>下的WAS管理控制台中配置TestTopicTestTopicCF JMS>主题连接工厂和主题部分。运行应用程序时没有例外。 如果我使用创建的队列和队列连接工厂,它工作正常。 有什么东西我需要专门做才能使主题工作?

1 个答案:

答案 0 :(得分:1)

主题是与队列不同的目的地,默认情况下,当发布者发送消息时,它们不会保留消息并且必须连接订户。查看一些详细信息here

  

发布商和订阅者具有时间依赖性。一个客户   订阅主题只能消费后发布的消息   客户端已创建订阅,订阅者必须继续   积极地使它消费消息。

简而言之:

  • 您当前的设计对于主题不正确,您必须首先调用接收器servlet,有很长的接收超时,并在第二个窗口中尝试发送器servlet,因为您的消息现在刚丢失。
  • 更好的方法是使用MDB作为消息接收器而不是servlet
  • 如果您需要在订阅者处于非活动状态时接收发送到主题的消息,则需要在WAS中配置持久订阅者并将主题配置为持久。