使用weblogic jms定时发送的功能列子

package com.sourceware.sms.platform.jms;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.sourceware.sms.SMS;
import org.apache.log4j.Logger;

/**
* <p>标题: 短信平台</p>
* <p>描述: 短信平台以及相关业务。</p>
* <p>版权: 宁波友科软件有限公司 Copyright (c) 2004</p>
* <p>公司: 宁波友科软件有限公司</p>
* @author 吴锋彬([email protected]
* @version 1.0
*/

public class JMSQueueSend {

  Logger logger = Logger.getLogger(JMSQueueSend.class);
  public JMSQueueSend() {
  }

  public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
  private static String JMS_FACTORY = "JNDI_JMS_Connection_Factory";
  private static String QUEUE = null;

  private QueueConnectionFactory qconFactory;
  private QueueConnection qcon;
  private weblogic.jms.extensions.WLQueueSession qsession;
  private weblogic.jms.client.JMSProducer qsender;
  private Queue queue;
  private ObjectMessage msg;

  /**
   * Creates all the necessary objects for sending
   * messages to a JMS queue.
   *
   * @param ctx JNDI initial context
   * @param queueName name of queue
   * @exception NamingException if operation cannot be performed
   * @exception JMSException if JMS fails to initialize due to internal error
   */
  void init(Context ctx, String queueName) throws NamingException, JMSException {
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);

    qcon = qconFactory.createQueueConnection();

    qsession = (weblogic.jms.extensions.WLQueueSession) qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

    queue = (Queue) ctx.lookup(queueName);

    qsender = (weblogic.jms.client.JMSProducer) qsession.createSender(queue);

    msg = qsession.createObjectMessage();
    logger.debug(qconFactory + "["
                       + qcon.getClass().getName()
                       + "]"
                       + "["
                       + qsession.getClass().getName()
                       + "]"
                       + "["
                       + qsender.getClass().getName()
                       + "]"
                       + "["
                       + msg.getClass().getName()
                       + "]");

    qcon.start();
  }

  /**
   * Closes JMS objects.
   * @exception JMSException if JMS fails to close objects due to internal error
   */
  void close() throws JMSException {
    qsender.close();
    qsession.close();
    qcon.close();
  }

  /**
   *
   * @param SMS SMS
   * @throws Exception
   */
  public void sendMO(SMS sms) throws Exception {
    InitialContext ic = new InitialContext();
    init(ic, "JNDI_CMW_SMS_MO_QUEUE");
    msg.setJMSExpiration(sms.getExpiration());
    msg.setJMSMessageID(new Long(sms.getMessageID()).toString());
    long time2Deliver = sms.getSheduleTime().getTime() - System.currentTimeMillis();
    if (time2Deliver > 0) {
      qsender.setTimeToDeliver(time2Deliver);//定时
    }

    msg.setObject(sms);
    msg.setJMSPriority(sms.getPriority());
    qsender.send(msg);
    this.close();

  }

  /**
   *
   * @param SMS SMS
   * @throws Exception
   */
  public void sendMT(SMS sms) throws Exception {
   
    InitialContext ic = new InitialContext();
    init(ic, "JNDI_CMW_SMS_MT_QUEUE");
    msg.setJMSExpiration(sms.getExpiration());
    msg.setJMSMessageID(new Long(sms.getMessageID()).toString());
    long time2Deliver = sms.getSheduleTime().getTime() - System.currentTimeMillis();
    if (time2Deliver > 0) {
      qsender.setTimeToDeliver(time2Deliver);
    }
    msg.setObject(sms);
    msg.setJMSPriority(sms.getPriority());
    qsender.send(msg);
    this.close();

  }

  /**
   *
   * @param sms SMS
   * @param jndiName String
   * @throws Exception
   */
  public void send(SMS sms, String jndiName) throws Exception {
    InitialContext ic = new InitialContext();
    init(ic, jndiName);
    msg.setJMSExpiration(sms.getExpiration());
    msg.setJMSMessageID(new Long(sms.getMessageID()).toString());
    long time2Deliver = sms.getSheduleTime().getTime() - System.currentTimeMillis();
    if (time2Deliver > 0) {
      qsender.setTimeToDeliver(time2Deliver);
    }

    msg.setObject(sms);
    msg.setJMSPriority(sms.getPriority());
    qsender.send(msg);
    this.close();

  }
}


你可能感兴趣的:(apache,c,log4j,weblogic,jms)