Passing events in JMS

Posted by sam on Stack Overflow See other posts from Stack Overflow or by sam
Published on 2009-07-02T18:33:27Z Indexed on 2010/04/12 23:12 UTC
Read the original article Hit count: 511

Filed under:
|
|
|

I'm new in JMS. In my program, My Problem is that , I want to pass 4 events(classes) (callEvent, agentEvent, featureEvent, eventListenerExit) from the JMSQueue Program , who i m mention below. How can I do this?

// (JmsSender.java)

package com.apac.control.helper; 

import java.util.Calendar;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

import com.apac.control.api.CallData;
import com.apac.control.exception.CtiException;

import library.cti.CtiEventDocument;
import library.cti.impl.CtiEventDocumentImpl;

public final class JmsSender 
{ 
    private QueueConnectionFactory factory;
    private Queue queue;

    private QueueConnection connection;
    private QueueSession session;

    private QueueSender sender;

    private String sessionId;
    private String deviceId;
    private String centerId;
    private String switchId;

    public JmsSender(String queueJndiName, String sessionId, String deviceId,
            String centerId, String switchId) throws CtiException
    {
        this.sessionId = sessionId;
        this.deviceId = deviceId;
        this.centerId = centerId;
        this.switchId = switchId;

        try {
            InitialContext ic = new InitialContext();
            factory = (QueueConnectionFactory) 
                ic.lookup("javax/jms/QueueConnectionFactory");
            queue = (Queue) ic.lookup(queueJndiName);

        } catch (Exception e) {
            throw new CtiException("CTI. Error creating JmsSender.", e);
        }
    }

    public String getCenterId()
    {
        return centerId;
    }

    public String getDeviceId()
    {
        return deviceId;
    }

    public String getSwitchId()
    {
        return switchId;
    }


    public void connect() throws CtiException {
        try {

            connection = factory.createQueueConnection();

        } catch (Exception e) {
            throw new CtiException("CTI000. Error connecting to cti queue.");
        }
    }

    public void close() throws CtiException {
        try {
            connection.close();

        } catch (Exception e) {
            throw new CtiException("CTI000. Error closing queue.");
        }
    }

    public void send(String eventType, CallData call, long seqId) throws CtiException {

        // prepare the message

        CtiEventDocument ced = this.createBaseCtiDocument();
        CtiEventDocument ce = ced.getCtiEvent();

        ce.setSequenceId(seqId);
        ce.setCallId("" + call.getCallId());
        ce.setUcid(call.getUCID());
        ce.setEventType(eventType);
        ce.setDnisNumber(call.getDnisNumber());
        ce.setAniNumber(call.getAniNumber());
        ce.setApplicationData(call.getApplicationData());
        ce.setQueueNumber(call.getQueueNumber());
        ce.setCallingNumber(call.getCallingNumber());
        if (call instanceof ManualCall) {
            ce.setManual("yes");
        }

        try {
            sendMessage(ced.toString());
        } catch (Exception e) {
            throw new CtiException("CTI051. Error sending message.", e);
        }
    }

    public void send(String eventType, String agentId, String agentMode, long seqId) 
            throws CtiException
    {
        CtiEventDocument ced = this.createBaseCtiDocument();
        CtiEventDocument ce = ced.getCtiEvent();

        ce.setSequenceId(seqId);
        ce.setEventType(eventType);
        ce.setAgentId(agentId);
        ce.setAgentMode(agentMode);

        try {
            sendMessage(ced.toString());
        } catch (Exception e) {
            throw new CtiException("CTI051. Error sending message.", e);
        }
    }

    public void sendError(String errCode, String errMsg) throws CtiException
    {
        CtiEventDocument ced = this.createBaseCtiDocument();
        CtiEventDocument ce = ced.getCtiEvent();

        ce.setEventType("Error");
        ce.setErrorCode(errCode);
        ce.setErrorMessage(errMsg);

        try {
            sendMessage(ced.toString());
        } catch (Exception e) {
            throw new CtiException("CTI051. Error sending message.", e);
        }
    }

    private CtiEventDocument createBaseCtiDocument() {
        CtiEventDocument ced = CtiEventDocument.Factory.newInstance();
        CtiEventDocument ce = ced.addNewCtiEvent();

        ce.setSessionId(sessionId);
        ce.setSwitchId(switchId);
        ce.setCenterId(centerId);
        ce.setDeviceId(deviceId);
        ce.setTime(Calendar.getInstance());

        return ced;
    }

    // Synchronization protects session, which cannot be
    // accessed by more than one thread. We may more than
    // one thread here from Cti in some cases (for example
    // when customer is being transfered out and hangs the call
    // at the same time.
    synchronized void sendMessage(String msg) throws Exception
    {
        session = connection.createQueueSession(true,
            Session.AUTO_ACKNOWLEDGE);
        sender = session.createSender(queue);

        TextMessage txtMsg = session.createTextMessage(msg);

        sender.send(txtMsg);
        sender.close();
        session.commit();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about jms