2

So I have this spring integration project. That use a gateway as a trigger to spring batch jobs. I made this interface for the gateway:

public interface TestGateway {
    void trigger(String pass);
}

Then a java class that triggers the interface:

public class Trigger implements Runnable {

@Autowired
TestGateway testGateWay;

public void triggerMethod() {

    testGateWay.trigger("pass");
}

@Override
public void run() {
    try {
        triggerMethod();
    } catch (Exception e) {
        e.printStackTrace();
       }
    }
 }

every time I tried to run it an exception appear:

java.lang.NullPointerException
at com.irsis.integration.endpoint.Trigger.triggerMethod(Trigger.java:12)
at com.irsis.integration.endpoint.Trigger.run(Trigger.java:18)
at org.springframework.scheduling.support.DelegatingErrorHandling

line 22 is: testGateWay.trigger("pass");

my integration-context.xml

    <context:component-scan base-package="com.irsis.integration.endpoint" />

<int:logging-channel-adapter id="logger"
    log-full-message="true" level="INFO" />

<!-- gateway -->
<int:channel id="testInput" />

<int:gateway id="testGateway"
    service-interface="com.irsis.integration.endpoint.TestGateway">
    <int:method name="trigger" request-channel="testInput" />
</int:gateway>

<int:channel id="activate" />

<import resource="classpath*:/spring/batch/jobs/testJob.xml" />

<int:transformer input-channel="testInput"
    output-channel="activate">
    <bean class="com.irsis.integration.util.TriggerToJobRequest">
        <property name="job" ref="testJob" />
    </bean>
</int:transformer>



<int:service-activator method="launch"
    input-channel="activate">
    <bean id="messageHandler"
        class="org.springframework.batch.integration.launch.JobLaunchingMessageHandler">
        <constructor-arg ref="jobLauncher" />
    </bean>
</int:service-activator>

Start of my application-context.xml

<context:annotation-config />
<context:spring-configured />

<import resource="classpath*:/spring/integration/si-batch-config.xml" />
    <import resource="classpath*:/spring/integration/si-test2.xml" />


<context:component-scan base-package="com.irsis" />

Any ideas why?

Thanks,
Jet

  • 3
    Do you have a class implementing TestGateway? If yes, does it have the @Component annotation? – sp00m Mar 21 '14 at 09:41
  • no, I don't have one. because as far as I know in spring integration the gateway interface doesn't have a implementing class. And I already tested autowiring an interface without an implementing class on my first integration class and it works. – Jet Waitforit Torres Mar 21 '14 at 09:45
  • You wire the interface, but it needs an implementation: http://stackoverflow.com/questions/12899372/spring-why-do-we-autowire-the-interface-and-not-the-implemented-class Look at it like this: what do you want spring to do when calling: `void trigger(String pass);` – JohanB Mar 21 '14 at 09:48

2 Answers2

0

Since you say that you use Spring Integration, your gateway interface should have 'implementation' - <gateway>

Something like this:

<gateway id="testGateway" service-interface="com.my.proj.TestGateway" 
          default-request-channel="gatewayChannel"/>
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • OK. Is your `Trigger` a Spring bean? How does your application context looks? Is it really a full Spring Integration context? There is not enough info to determine why your app doesn't have a Spring Integration proxy implementation for your interface. – Artem Bilan Mar 21 '14 at 09:50
  • updated my question: added the integration xml. the method name="trigger' is this line in my interface 'void trigger(String pass);' – Jet Waitforit Torres Mar 21 '14 at 09:54
  • Show, please, your `Trigger` bean, and how you start the application context. I may guess, but it seems like you don't include your `integration-context.xml` to the entire aapplication context. And the last one doesn't have beans like `testInput`, `activate` etc. – Artem Bilan Mar 21 '14 at 09:59
  • No my Trigger is not a spring bean but the method inside the interface. testInput was the channel that contains the gateway, then from the gateway it will transform it as a job. then the activate uses it to launch the job. Also updated the question to see some part of the application-context.xml. The first import was the first integration I made and is working. Im currently following that pattern. – Jet Waitforit Torres Mar 21 '14 at 10:07
  • If `Trigger` isn't a Spring bean how do you want to coerce the Spring container to inject anything to the field with `@Autowired` ? – Artem Bilan Mar 21 '14 at 10:23
  • OK. They explain for us, please, what is this: `java.lang.NullPointerException at com.irsis.integration.endpoint.Trigger.triggerMethod(Trigger.java:12)` – Artem Bilan Mar 21 '14 at 10:38
  • That's the reason why I posted this question. I don't know why it's getting a npe. – Jet Waitforit Torres Mar 21 '14 at 10:40
  • OK. One more time: is your class `com.irsis.integration.endpoint.Trigger` a Spring bean, if you are trying to inject anything from Spring container using `@Autowired` ? It is a couse of `NPE` though. You can do this `new Trigger()`, but it doesn't say that Spring will do the stuff for you – Artem Bilan Mar 21 '14 at 10:44
  • Ok sorry my bad. It had the same name inside the method of my interface. Bad programming. That's why I got confuse. Yes, I think it is a spring bean. I added @Component annotation at the trigger class. – Jet Waitforit Torres Mar 21 '14 at 10:50
  • 1
    Good. Now how do you use it: `new Trigger()` or `ctx.getBean("trigger")` ? – Artem Bilan Mar 21 '14 at 10:52
  • I know what went wrong. Thanks was using new Trigger(); – Jet Waitforit Torres Mar 21 '14 at 11:03
0

You have a typo in your code, which might be a problem:

TestGateway testGateWay

try changing it to

TestGateway testGateway
papacito
  • 369
  • 7
  • 18