⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptpreceiver.java

📁 21天学通J2EE的例子4
💻 JAVA
字号:
import javax.naming.*;
import javax.jms.*;

public class PTPReceiver {

    private Connection connection;
    private Session session;
    private MessageConsumer consumer;

    public static void main(String[] args) {
        PTPReceiver receiver = null;
        try {
            receiver = new PTPReceiver();
            String textMsg;
            textMsg = receiver.consumeMessage();
            System.out.println ("Received: " + textMsg);
        }
        catch(Exception ex) {
             System.err.println("Exception in PTPReceiver: " + ex);
        }
        finally {
            try {receiver.close();} catch(Exception ex){}
        }
    }

    public PTPReceiver() throws JMSException, NamingException {
        Context context = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("jms/QueueConnectionFactory");
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = (Destination)context.lookup("jms/firstQueue");
        consumer = session.createConsumer(destination);
        connection.start();
    }

    public String consumeMessage () throws JMSException {
        String text = null;
        Message msgBody = consumer.receive();
        if (msgBody instanceof TextMessage) {
            text = ((TextMessage) msgBody).getText();
        }
        else {
          text = msgBody.toString();
        }
        return text;
    }

    public void close() throws JMSException {
        connection.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -