summaryrefslogtreecommitdiffstats
path: root/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSService.java
blob: d92303145edbdedfe9d3b2942493cfba4ff737c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.apache.tuscany.binding.jms;


import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.naming.NamingException;
import javax.xml.namespace.QName;

import org.apache.tuscany.spi.component.CompositeComponent;
import org.apache.tuscany.spi.extension.ServiceExtension;
import org.apache.tuscany.spi.wire.WireService;

import commonj.sdo.helper.TypeHelper;

/**
 * @version $Rev$ $Date$
 */
public class JMSService extends ServiceExtension {

	private JMSBinding jmsBinding;
    private TypeHelper typeHelper;
	private JMSResourceFactory jmsResourceFactory;
    private MessageConsumer consumer;
	private OperationSelector operationSelector;
    
    public JMSService(String name,
                      CompositeComponent parent,
                      WireService wireService,
                      JMSBinding jmsBinding,
                      JMSResourceFactory jmsResourceFactory,
                      OperationSelector operationSelector,
                      Class<?> service,
                      TypeHelper typeHelper) {
        super(name, service, parent, wireService);

        this.jmsBinding = jmsBinding;
        this.typeHelper = typeHelper;
        this.jmsResourceFactory = jmsResourceFactory;
        this.operationSelector = operationSelector;
    }

    public void start() {
        super.start();
        try {
			registerListerner();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void stop() {
            	
    	try {
    		consumer.close();
    		jmsResourceFactory.closeConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	super.stop();
    }
    
    private void registerListerner() throws NamingException, JMSException{
    	
    	Object entryPointProxy = this.getServiceInstance();
    	QName responseQN = new QName("payload");
    	SDODataBinding dataBinding = new SDODataBinding(typeHelper, false, responseQN);
    	
    	Session session = jmsResourceFactory.createSession();
        Destination destination = session.createQueue(jmsBinding.getDestinationName());
        
        consumer = session.createConsumer(destination);
        consumer.setMessageListener(new JMSProxy(entryPointProxy,jmsResourceFactory,jmsBinding,dataBinding,operationSelector));
        
        jmsResourceFactory.startConnection();
        
    }    
}