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

📄 messagesender.java

📁 Java项目案例导航
💻 JAVA
字号:
package examples;import java.io.*;import java.util.*;import javax.transaction.*;import javax.naming.*;import javax.jms.*;/** * This class sends JMS Text message to the topic, at present cartbean uses this class to  * send JMS message to topic.  */public class MessageSender{        /*     * The JNDI context factory.     */    public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";        /*     * The JMS connection factory JNDI name.     */    public final static String JMS_FACTORY="javax.jms.TopicConnectionFactory";        /*     * The JMS topic JNDI name.     */    public final static String TOPIC="testtopic";        /*     * The Server URL     */    public final static String URL="t3://localhost:7001";        /*     * The JMS connection factory     */    protected TopicConnectionFactory tconFactory;        /*     * The JMS topic connection     */    protected TopicConnection tcon;        /*     * The JMS topic session     */    protected TopicSession tsession;        /*     * The JMS topic publisher     */    protected TopicPublisher tpublisher;        /*     *  The JMS topic      */    protected Topic topic;        /*     * The JMS TextMessage     */    protected TextMessage msg;        /**     * Creates all the necessary objects for sending     * messages to a JMS Topic.     *      * @param ctx JNDI initial context     * @param topicName name of topic     * @exception NamingExcpetion if problem occurred with the JNDI context interface      * @exception JMSException if JMS fails to initialize due to internal error     *     */    public void init(Context ctx, String topicName)       throws NamingException, JMSException{                       /*             * The Topic connection factory JNDI lookup             */            tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);                        /*             * Creates a topic connection factory             */             tcon = tconFactory.createTopicConnection();                        /*             * Creates a topic session             */            tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);                        /**             * Topic JNDI lookup             */            topic = (Topic) ctx.lookup(topicName);                        /**             * Creates a publisher             */            tpublisher = tsession.createPublisher(topic);                        /**             * Creates the Textmessage             */            msg = tsession.createTextMessage();            tcon.start();    }        /**     * Creates all the necessary  JMS objects and Sends a message to a JMS topic.     *     * @params message message to be sent     * @exception JMSException if JMS fails to send message due to internal error     *     */    public void sendMessage(String message)  throws JMSException {      try{            InitialContext ctx= getInitialContext(URL);            init(ctx,TOPIC);            send(message);      }catch(Exception ex){          throw new JMSException( ex.getMessage());      }finally{          close();      }    }        /**     * Sends a message to a JMS topic.     *     * @params message message to be sent     */    public void send(String value)       throws JMSException {           msg.setText(value);           tpublisher.publish(msg);    }        /**     * Closes JMS objects.     *     * @exception JMSException if JMS fails to close objects due to internal error     */    public void close()       throws JMSException {           tpublisher.close();           tsession.close();           tcon.close();    }        /**     * Returns application server initialcontext     */        protected static InitialContext getInitialContext(String url)       throws NamingException {           Hashtable env = new Hashtable();           env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);           env.put(Context.PROVIDER_URL, url);           env.put("weblogic.jndi.createIntermediateContexts", "true");           return new InitialContext(env);    }    /**     * main() method.     *     * @param args WebLogic Server URL     * @exception Exception if operation fails     */    public static void main(String[] args)        throws Exception{            if (args.length != 1) {                System.out.println("Usage: java examples.jms.topic.TopicSend WebLogicURL");                return;            }            InitialContext ic = getInitialContext(args[0]);            MessageSender sender = new MessageSender();            sender.init(ic, TOPIC);            sender.send("123456");            sender.close();    }    }

⌨️ 快捷键说明

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