0

I am trying to pass reference of an interface as a property in a class (inheriting another class), I am getting an error. I tried same thing in a class which does not inherit anything and it works fine. I don't know if I am missing anything here. Any help is appreciated. Thanks :)

Class :

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.ScheduledPollConsumer;

import com.walmart.utils.storeConfig.api.IStoreConfigService;


public class WalPipeConsumer extends ScheduledPollConsumer
{
    final static private String CLASS_NAME = WalPipeConsumer.class.getName();

    final static private Logger logger = Logger.getLogger(CLASS_NAME);

    protected Hashtable pipeBuffers;

    private Vector msgs = new Vector();

    private int arrrayLength = 0, headerLength = 0;

    private final Endpoint endpoint;

    private final String pipe;

    private IStoreConfigService storeConfigService;

public WalPipeConsumer(Endpoint endpoint, Processor processor, String pipe)
        throws Exception
{
    super(endpoint, processor);
    logger.finest("inside the wal pipe consumer constructor");
    this.pipe = pipe;
    this.endpoint = endpoint;
    this.countryCode = countryCode;
    logger.finest("before starting the thread");
    new Thread(new WalPipeInputRunner(), "WalCamel Pipe Consumer[" + pipe
            + "]").start();
    logger.finest("after starting the thread");
}

// There are some more functions 
/**
 * @param storeConfigServicethe storeConfigService to set
 */
public void setStoreConfigService(IStoreConfigService storeConfigService) {
    this.storeConfigService = storeConfigService;
}

Blueprint.xml:

<reference id="storeConfigService"
    interface="com.walmart.utils.storeConfig.api.IStoreConfigService" />

<bean id="storeConfigAdapter" class="com.tgcs.walpipe.endpoint.StoreConfigAdapter" init-method="init">
    <property name="storeConfigService" ref="storeConfigService" />
</bean>

<bean id="WalPipeConsumer" class="com.tgcs.walpipe.endpoint.WalPipeConsumer">
    <property name="storeConfigService" ref="storeConfigService" /> 
</bean>

Error:

[2017.09.15-10:34:00.788] [SEVERE] 
[org.apache.aries.blueprint.container.BlueprintContainerImpl] 
[org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun] - Unable 
to start blueprint container for bundle com.tgcs.walpipe.endpoint
org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to 
find a matching constructor on class 
com.tgcs.walpipe.endpoint.WalPipeConsumer for arguments [] when 
instanciating bean WalPipeConsumer 
Prashant Gupta
  • 103
  • 2
  • 12

1 Answers1

0

Blueprint tries to create an instance of WalPipeConsumer. You are using a property to set the storeConfigService. So blueprint will first call the empty Constructor to instantiate the class and then call the setter to set the storeConfigService.

As the class does not have an empty construtor this will fail.

To solve this you either need an empty constructor or use elements to set all parameters of the existing constructor.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Thanks Christian for your response. Now I am trying to create no-argument constructor and calling parent class constructor like "super(null, null)" inside it, since parent class does not have no-argument constructor. Now I am getting errors because I am passing nulls to super class constructor, can we have any work around on that ? – Prashant Gupta Sep 18 '17 at 20:29
  • The parent class cannot be used as a Blueprint bean, either directly or any subtype. If you cannot change the parent class then you will have to extend it by composition rather than inheritance -- that is, create a bean that does not inherit from `ScheduledPollConsumer` but instead contains a field of that type. – Neil Bartlett Sep 18 '17 at 20:55
  • Actually I am not sure if setting up a Consumer in blueprint makes sense at all. Camel normally creates consumers internally when needed. If all you want is to add a service ref to your consumer then you can get this ref from the camel registry. In OSGi it also contains OSGi services. – Christian Schneider Sep 20 '17 at 04:41