001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.directvm; 018 019import org.apache.camel.Consumer; 020import org.apache.camel.Processor; 021import org.apache.camel.Producer; 022import org.apache.camel.component.direct.DirectConsumer; 023import org.apache.camel.impl.DefaultEndpoint; 024import org.apache.camel.spi.Metadata; 025import org.apache.camel.spi.UriEndpoint; 026import org.apache.camel.spi.UriParam; 027import org.apache.camel.spi.UriPath; 028 029/** 030 * The direct-vm component provides direct, synchronous call to another endpoint from any CamelContext in the same JVM. 031 * 032 * This endpoint can be used to connect existing routes in the same JVM between different CamelContexts. 033 */ 034@UriEndpoint(scheme = "direct-vm", title = "Direct VM", syntax = "direct-vm:name", consumerClass = DirectConsumer.class, label = "core,endpoint") 035public class DirectVmEndpoint extends DefaultEndpoint { 036 037 @UriPath(description = "Name of direct-vm endpoint") @Metadata(required = "true") 038 private String name; 039 040 @UriParam(label = "producer") 041 private boolean block; 042 @UriParam(label = "producer", defaultValue = "30000") 043 private long timeout = 30000L; 044 @UriParam(label = "producer") 045 private boolean failIfNoConsumers = true; 046 047 public DirectVmEndpoint(String endpointUri, DirectVmComponent component) { 048 super(endpointUri, component); 049 } 050 051 @Override 052 public DirectVmComponent getComponent() { 053 return (DirectVmComponent) super.getComponent(); 054 } 055 056 @Override 057 public Producer createProducer() throws Exception { 058 if (block) { 059 return new DirectVmBlockingProducer(this); 060 } else { 061 return new DirectVmProducer(this); 062 } 063 } 064 065 @Override 066 public Consumer createConsumer(Processor processor) throws Exception { 067 Consumer answer = new DirectVmConsumer(this, new DirectVmProcessor(processor, this)); 068 configureConsumer(answer); 069 return answer; 070 } 071 072 @Override 073 public boolean isSingleton() { 074 return true; 075 } 076 077 public DirectVmConsumer getConsumer() { 078 return getComponent().getConsumer(this); 079 } 080 081 public boolean isBlock() { 082 return block; 083 } 084 085 /** 086 * If sending a message to a direct endpoint which has no active consumer, 087 * then we can tell the producer to block and wait for the consumer to become active. 088 */ 089 public void setBlock(boolean block) { 090 this.block = block; 091 } 092 093 public long getTimeout() { 094 return timeout; 095 } 096 097 /** 098 * The timeout value to use if block is enabled. 099 */ 100 public void setTimeout(long timeout) { 101 this.timeout = timeout; 102 } 103 104 public boolean isFailIfNoConsumers() { 105 return failIfNoConsumers; 106 } 107 108 /** 109 * Whether the producer should fail by throwing an exception, when sending to a DIRECT-VM endpoint with no active consumers. 110 */ 111 public void setFailIfNoConsumers(boolean failIfNoConsumers) { 112 this.failIfNoConsumers = failIfNoConsumers; 113 } 114 115}