JmsTestClinet2.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:7k
源码类别:
Java编程
开发平台:
Java
- package userjmssesenbdb;
- import javax.jms.*;
- import javax.naming.*;
- import java.util.Hashtable;
- import java.io.Serializable;
- import java.util.Properties;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- *
- * Use this class to send and receive point-to-point messages.
- * To send a text message:
- * <code>
- * JmsTestClinet2 jmsTestClinet2 = new JmsTestClinet2();
- * jmsTestClinet2.sendText("Hello world");
- * jmsTestClinet2.close(); //Release resources
- * </code>
- *
- * <code>
- * To receive a message:
- * JmsTestClinet2 jmsTestClinet2 = new JmsTestClinet2();
- * jmsTestClinet2.getQueueReceiver();
- * </code>
- */
- public class JmsTestClinet2 implements MessageListener {
- private static Context context = null;
- private boolean transacted = true;
- private int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;
- private QueueConnectionFactory queueConnectionFactory = null;
- private QueueConnection queueConnection = null;
- private QueueSession queueSession = null;
- private QueueSender queueSender = null;
- private QueueReceiver queueReceiver = null;
- private Queue queue = null;
- private String queueConnectionFactoryName = "DemoJMSConnectionFactory";
- private String sendQueueName = "DemoJMSQueue";
- private String recvQueueName = "DemoJMSQueue";
- public boolean isTransacted() {
- return transacted;
- }
- public void setTransacted(boolean transacted) {
- this.transacted = transacted;
- }
- public int getAcknowledgementMode() {
- return acknowledgementMode;
- }
- public void setAcknowledgementMode(int acknowledgementMode) {
- this.acknowledgementMode = acknowledgementMode;
- }
- public String getQueueConnectionFactoryName() {
- return queueConnectionFactoryName;
- }
- public void setQueueConnectionFactoryName(String queueConnectionFactoryName) {
- this.queueConnectionFactoryName = queueConnectionFactoryName;
- }
- public String getSendQueueName() {
- return sendQueueName;
- }
- public void setSendQueueName(String sendQueueName) {
- this.sendQueueName = sendQueueName;
- }
- public String getRecvQueueName() {
- return recvQueueName;
- }
- public void setRecvQueueName(String recvQueueName) {
- this.recvQueueName = recvQueueName;
- }
- private Context getInitialContext() throws Exception {
- String url = "t3://TsingHuaSQL:7001";
- String user = null;
- String password = null;
- Properties properties = null;
- try {
- properties = new Properties();
- properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
- properties.put(Context.PROVIDER_URL, url);
- if (user != null) {
- properties.put(Context.SECURITY_PRINCIPAL, user);
- properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
- }
- return new InitialContext(properties);
- }
- catch(Exception e) {
- System.out.println("Unable to connect to WebLogic server at " + url);
- System.out.println("Please make sure that the server is running.");
- throw e;
- }
- }
- private Context getContext() throws Exception {
- if (context == null) {
- try {
- context = getInitialContext();
- }
- catch(Exception ex) {
- ex.printStackTrace();
- throw ex;
- }
- }
- return context;
- }
- public QueueConnectionFactory getQueueConnectionFactory() throws Exception {
- if (queueConnectionFactory == null) {
- Object obj = getContext().lookup(queueConnectionFactoryName);
- queueConnectionFactory = (QueueConnectionFactory) obj;
- }
- return queueConnectionFactory;
- }
- public QueueConnection getQueueConnection() throws Exception {
- if (queueConnection == null) {
- queueConnection = getQueueConnectionFactory().createQueueConnection();
- queueConnection.start();
- }
- return queueConnection;
- }
- public QueueSession getQueueSession() throws Exception {
- if (queueSession == null) {
- queueSession = getQueueConnection().createQueueSession(isTransacted(), getAcknowledgementMode());
- }
- return queueSession;
- }
- public Queue getSendQueue() throws Exception {
- if (queue == null) {
- Object obj = getContext().lookup(sendQueueName);
- queue = (Queue) obj;
- }
- return queue;
- }
- public Queue getRecvQueue() throws Exception {
- if (queue == null) {
- Object obj = getContext().lookup(recvQueueName);
- queue = (Queue) obj;
- }
- return queue;
- }
- public QueueSender getQueueSender() throws Exception {
- if (queueSender == null) {
- queueSender = getQueueSession().createSender(getSendQueue());
- }
- return queueSender;
- }
- public QueueReceiver getQueueReceiver() throws Exception {
- if (queueReceiver == null) {
- queueReceiver = getQueueSession().createReceiver(getRecvQueue());
- queueReceiver.setMessageListener(this);
- }
- return queueReceiver;
- }
- public void sendText(String message) throws Exception {
- TextMessage textMessage = getQueueSession().createTextMessage();
- textMessage.clearBody();
- textMessage.setText(message);
- getQueueSender().send(textMessage);
- if (isTransacted()) {
- getQueueSession().commit();
- }
- }
- public void sendObject(Serializable message) throws Exception {
- ObjectMessage objectMessage = getQueueSession().createObjectMessage();
- objectMessage.clearBody();
- objectMessage.setObject(message);
- getQueueSender().send(objectMessage);
- if (isTransacted()) {
- getQueueSession().commit();
- }
- }
- public void onMessage(Message message) {
- if (message instanceof BytesMessage) {
- BytesMessage bytesMessage = (BytesMessage) message;
- //Process bytesMessage here
- }
- else {
- if (message instanceof MapMessage) {
- MapMessage mapMessage = (MapMessage) message;
- //Process mapMessage here
- }
- else {
- if (message instanceof ObjectMessage) {
- ObjectMessage objectMessage = (ObjectMessage) message;
- //Process objectMessage here
- }
- else {
- if (message instanceof StreamMessage) {
- StreamMessage streamMessage = (StreamMessage) message;
- //Process streamMessage here
- }
- else {
- if (message instanceof TextMessage) {
- TextMessage textMessage = (TextMessage) message;
- //Process textMessage here
- }
- }
- }
- }
- }
- try {
- if (isTransacted()) {
- getQueueSession().commit();
- }
- }
- catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- public void close() throws Exception {
- if (queueSender != null) {
- queueSender.close();
- }
- if (queueReceiver != null) {
- queueReceiver.close();
- }
- if (queueSession != null) {
- queueSession.close();
- }
- if (queueConnection != null) {
- queueConnection.close();
- }
- }
- }