`
Dead_knight
  • 浏览: 1200936 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
752c8642-b795-3fe6-946e-a4e845bffdec
Spring Securi...
浏览量:240190
33caa84e-18a6-3036-a82b-6e2106a4de63
clojure专题
浏览量:48914
E17ca077-44df-3816-a3fe-471c43f6e1e5
WebLogic11g
浏览量:236872
社区版块
存档分类
最新评论

性能优化(案例二)-JMS缓冲短信网关

阅读更多

问题:

移动开通一个短信网关供系统发送短信,后来随着业务增加、以及外系统短信接口接入,导致本系统短信模块处理量急剧增加,每秒发送短信50-80条,每逢访问高峰期,导致短信网关堵塞。

 

其次是短信模块部署在应用系统现网环境中,应用系统与短信模块性能上相互影响,不稳定。

 

方案:

由于短信模块直接与短信网关打交道,而应用系统、外系统想发送短息直接与短信模块打交道

可单独部署一个短信服务域,本系统、外系统直接访问短信服务域公布的接口访问即可。

通过在短信模块与短信网关中间建立一个缓冲池(JMS)缓冲短消息

 

实施:

步骤一、

服务域的部署(省略)

 

步骤二、

配置JMS(省略)

 

步骤三、

改造原先短信发送的代码

JMS连接初始化:

    private QueueConnection connection;
    private QueueSession session;
    private QueueSender sender;

    public void initialize() throws Exception
    {
        if (log.isInfoEnabled()) {
            log.info("Starting JMS Queue Sender Service...");
        }
       
        try
        {
            InitialContext jndi = getInitialContext("t3://host:port");
            QueueConnectionFactory factory = (QueueConnectionFactory)jndi.lookup("QueueConnectionFactory");
            connection = factory.createQueueConnection();
            session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            Queue queue = (Queue)jndi.lookup("SMSQueue");  
            sender = session.createSender(queue);
            connection.start();

        }
        catch(Exception e)
        {
            throw e;
        }
    }

    //发送短信数据方法

    public void sendNotification(Serializable message)
    {
        try
        {
            ObjectMessage object = session.createObjectMessage();
            object.setObject(message);
            sender.send(object);
            log.info("Message Sender send object:" + object.getObject());
        }
        catch(JMSException e)
        {
            log.error("A problem was encountered when create Object Message", e);
            e.printStackTrace();
        }
    }

 

步骤四、

短信服务域增加消费JMS消息的监听器

    public void initialize() throws Exception
    {
        if (log.isInfoEnabled()) {
            log.info("Starting JMS Queue Receiver Service...");
        }
       
        try
        {
            InitialContext jndi = new InitialContext();
            QueueConnectionFactory factory = (QueueConnectionFactory)jndi.lookup("QueueConnectionFactory");
            connection = factory.createQueueConnection();
            session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            Queue queue = (Queue)jndi.lookup("SMSQueue");  
            sender = session.createSender(queue);
            connection.start();
            QueueReceiver receiver = session.createReceiver(queue);
            receiver.setMessageListener(new MessageListener(){
                public void onMessage(Message message)
                {
                    try
                    {
                        ObjectMessage objectMessage = null;
                       
                        if (!(message instanceof ObjectMessage)) {
                            log.error("Cannot handle message of type (class=" + message.getClass().getName() + "). Notification ignored.");
                            return;
                        }

                        objectMessage = (ObjectMessage) message;

                        //check the message content
                        if (!(objectMessage.getObject() instanceof MessageEntity)) {
                            log.error("An unknown cluster notification message received (class=" + objectMessage.getObject().getClass().getName() + "). Notification ignored.");
                            return;
                        }

                        if (log.isDebugEnabled()) {
                            log.debug(objectMessage.getObject());
                        }
                        //实际调用短信网关发送短信的方法

                        handleNotification(objectMessage.getObject());
                    } catch (JMSException jmsEx) {
                        log.error("Cannot handle cluster Notification", jmsEx);
                    }
                }
               
            });
        }
        catch(Exception e)
        {
            throw e;
        }
    }

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics