org.eclipse.swordfish.samples.dynamic.consumer.SimpleClient.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swordfish.samples.dynamic.consumer.SimpleClient.java

Source

/*******************************************************************************
 * Copyright (c) 2008, 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.swordfish.samples.dynamic.consumer;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.servicemix.jbi.jaxp.SourceTransformer;
import org.apache.servicemix.jbi.runtime.impl.MessageExchangeImpl;
import org.apache.servicemix.nmr.api.Channel;
import org.apache.servicemix.nmr.api.Endpoint;
import org.apache.servicemix.nmr.api.Exchange;
import org.apache.servicemix.nmr.api.Message;
import org.apache.servicemix.nmr.api.NMR;
import org.apache.servicemix.nmr.api.Pattern;
import org.apache.servicemix.nmr.core.MessageImpl;
import org.eclipse.swordfish.core.resolver.ServiceResolver;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

public class SimpleClient implements InitializingBean {

    private static final Log LOG = LogFactory.getLog(SimpleClient.class);

    private String dataToSend;
    private String interfaceName;
    private String consumerPolicyName;
    private String operationName;
    private Integer delayBeforeSending = 8000;
    private NMR nmr;

    public SimpleClient() {
    }

    private void checkConstraints() {
        Assert.notNull(dataToSend, "dataToSend property is compulsory");
        Assert.notNull(nmr, "nmr property is compulsory");
    }

    public void start() {
        checkConstraints();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    sendRequestSynchronously();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        }, delayBeforeSending);

    }

    public void sendRequestSynchronously() throws Exception {
        Channel dc = nmr.createChannel();
        Exchange ex = dc.createExchange(Pattern.InOut);
        Message msg = new MessageImpl();
        StreamSource src = new StreamSource(new ByteArrayInputStream(dataToSend.getBytes()));
        msg.setBody(src);
        ex.setIn(msg);
        ex.setProperty(MessageExchangeImpl.INTERFACE_NAME_PROP, QName.valueOf(interfaceName));
        if (consumerPolicyName != null && consumerPolicyName.length() > 0) {
            ex.setProperty(ServiceResolver.POLICY_CONSUMER_NAME, QName.valueOf(consumerPolicyName));
        }
        ex.setOperation(QName.valueOf(operationName));

        // TODO please find more suitable solution
        Map<String, Object> targetProps = new HashMap<String, Object>();
        targetProps.put(Endpoint.ENDPOINT_NAME, "JustDummyEndpointName");
        ex.setTarget(nmr.getEndpointRegistry().lookup(targetProps));

        LOG.info("!!SimpleClient is sending synchronous request with in message " + dataToSend);

        dc.sendSync(ex);
        if (ex.getError() != null) {
            LOG.error("The invocation wasn't successful", ex.getError());
        }
        if (ex.getFault() != null && ex.getFault().getBody() != null) {
            LOG.error("The invocation wasn't successful " + ex.getFault().getBody().toString());
        }
        LOG.info("!!SimpleClient have received the response: "
                + new SourceTransformer().toString(ex.getOut().getBody(Source.class)));
    }

    public String getDataToSend() {
        return dataToSend;
    }

    public void setDataToSend(String dataToSend) {
        this.dataToSend = dataToSend;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public String getConsumerPolicyName() {
        return consumerPolicyName;
    }

    public void setConsumerPolicyName(String consumerPolicyName) {
        this.consumerPolicyName = consumerPolicyName;
    }

    public String getOperationName() {
        return operationName;
    }

    public void setOperationName(String operationName) {
        this.operationName = operationName;
    }

    public Integer getDelayBeforeSending() {
        return delayBeforeSending;
    }

    public void setDelayBeforeSending(Integer delayBeforeSending) {
        this.delayBeforeSending = delayBeforeSending;
    }

    public NMR getNmr() {
        return nmr;
    }

    public void setNmr(NMR nmr) {
        this.nmr = nmr;
    }

    public void afterPropertiesSet() throws Exception {
        start();
    }

}