1 17 package org.apache.servicemix.client; 18 19 import javax.jbi.JBIException; 20 21 import org.apache.servicemix.id.IdGenerator; 22 import org.apache.servicemix.jbi.container.ActivationSpec; 23 import org.apache.servicemix.jbi.container.JBIContainer; 24 import org.apache.servicemix.jbi.nmr.flow.jms.JMSFlow; 25 26 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 27 28 35 public class RemoteServiceMixClient extends DefaultServiceMixClient { 36 37 private JBIContainer container; 38 private ActivationSpec activationSpec; 39 private String uri; 40 private JMSFlow jmsFlow; 41 private AtomicBoolean initialized = new AtomicBoolean(false); 42 private AtomicBoolean started = new AtomicBoolean(false); 43 44 49 public RemoteServiceMixClient(){ 50 this("peer://org.apache.servicemix?persistent=false"); 51 } 52 53 58 public RemoteServiceMixClient(String uri){ 59 this(uri, new ActivationSpec()); 60 } 61 62 67 public RemoteServiceMixClient(String uri, ActivationSpec activationSpec){ 68 container = new JBIContainer(); 69 container.setEmbedded(true); 70 container.setUseMBeanServer(false); 71 container.setName(new IdGenerator().generateSanitizedId()); 72 this.uri = uri; 73 this.activationSpec = activationSpec; 74 75 } 76 77 82 public void init() throws JBIException { 83 if (initialized.compareAndSet(false, true)) { 84 jmsFlow = new JMSFlow(); 85 jmsFlow.setJmsURL(uri); 86 container.setFlow(jmsFlow); 87 container.setEmbedded(true); 88 container.setUseMBeanServer(false); 89 container.setCreateMBeanServer(false); 90 container.setMonitorDeploymentDirectory(false); 91 container.setMonitorInstallationDirectory(false); 92 container.init(); 93 activationSpec.setComponent(this); 94 container.activateComponent(activationSpec); 95 } 96 } 97 98 104 public void start() throws JBIException { 105 start(Long.MAX_VALUE); 106 } 107 108 public void start(long timeout) throws JBIException { 109 init(); 110 if (started.compareAndSet(false, true)) { 111 container.start(); 112 if (timeout > 0) { 113 long start = System.currentTimeMillis(); 117 while (jmsFlow.numberInNetwork() == 0 && 118 System.currentTimeMillis() - start < timeout) { 119 try { 120 Thread.sleep(50); 121 } catch (InterruptedException e) { 122 throw new JBIException(e); 123 } 124 } 125 if (jmsFlow.numberInNetwork() == 0) { 126 throw new JBIException("Timeout while connecting to remote JBI container"); 127 } 128 } 129 super.start(); 130 } 131 } 132 133 139 public void stop() throws JBIException { 140 super.stop(); 141 } 142 143 149 public void shutDown() throws JBIException { 150 super.shutDown(); 151 container.shutDown(); 152 } 153 154 public String getContainerName() { 155 return container.getName(); 156 } 157 158 public void setContainerName(String name) { 159 container.setName(name); 160 } 161 162 public void close() throws JBIException { 163 shutDown(); 164 } 165 166 } 167 | Popular Tags |