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

📄 ptpsender.java

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

public class PTPSender {

    private Connection connection;
    private Session session;
    private MessageProducer producer;

    public static void main(String[] args) {
        PTPSender sender = null;
        try {
            sender = new PTPSender();
            System.out.println ("Sending message Hello World");
            sender.sendMessage("Hello World");
        } 
        catch(Exception ex) {
            System.err.println("Exception in PTPSender: " + ex);
        }
        finally {
            try {sender.close();} catch(Exception ex){}
        }
    }

    public PTPSender() 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");
        producer = session.createProducer(destination);
    }

    public void sendMessage(String msg) throws JMSException {
        TextMessage message = session.createTextMessage();
        message.setText(msg);
        producer.send(message);
    }

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

⌨️ 快捷键说明

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