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

📄 sendmessage.java

📁 J2EE 技术 源码 书籍源代码(j2ee编程技术)
💻 JAVA
字号:
/*
 * Created on 2004-7-2
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.ejbstudy;

import java.io.IOException;

import javax.naming.*;

import javax.jms.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author haoyulong
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class SendMessage extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//TODO Method stub generated by Lomboz
		doPost(request,response);
	}
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//TODO Method stub generated by Lomboz
		try {
		      // Get access to JNDI
		      Context context = new InitialContext();

		      // Lookup the managed connection factory for a topic
		      TopicConnectionFactory topicFactory = 
		         (TopicConnectionFactory)context.lookup("ConnectionFactory");

		      // Create a connection to the JMS provider
		      TopicConnection topicConnection = topicFactory.createTopicConnection();
		      
		      // Creat a topic session
		      TopicSession session = topicConnection.createTopicSession(
								   // No transaction
								   false, 
								   // Auto ack
								   Session.AUTO_ACKNOWLEDGE);

		     // Lookup the destination you want to publish to
		     Topic topic = (Topic)context.lookup("topic/MyTestTopic");

		     // Create a publisher
		     TopicPublisher pub = session.createPublisher(topic);

		     // Create a message
		     TextMessage message = session.createTextMessage();
		     message.setText("Hello World!");
		     message.setIntProperty("times",5);

		     // Publish the message
		     pub.publish(topic, message);

		     // Close the stuff
		     session.close();
		     topicConnection.close();
		     System.out.println("Servlet finished ");
		}
		    catch (Exception e) {
		      e.printStackTrace();
		    }

	}
}

⌨️ 快捷键说明

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