maryem neyli
maryem neyli

Reputation: 455

Nifi customized processor do not response and do not show any probleme

I'm working with Nifi and I'm trying to create an Apache Nifi processor but it doesn't react ! The processor is built, it shows up in the processors list and it works but without any inputs !! it does not show me anything.
Here is my code :

    @Override
    public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

        final AtomicReference<String> value = new AtomicReference<>();    
        Integer Somme=new Integer(1254+5520);

         FlowFile flowFile = session.get();
         session.read(flowFile, new InputStreamCallback() {
             @Override
             public void process(InputStream in) throws IOException {
                 HttpGet request = new HttpGet(URL);

                 HttpHost target = new HttpHost(localhost, 8080, "http");
                 CredentialsProvider provider = new BasicCredentialsProvider();
                 provider.setCredentials(
                         new AuthScope(target.getHostName(), target.getPort()),
                         new UsernamePasswordCredentials(Username, paswd)
                 );

                 AuthCache authCache = new BasicAuthCache();
                 authCache.put(target, new BasicScheme());

                 HttpClientContext localContext = HttpClientContext.create();
                 localContext.setAuthCache(authCache);

                 try (CloseableHttpClient httpClient = HttpClientBuilder.create()
                         .setDefaultCredentialsProvider(provider)
                         .build();
                      CloseableHttpResponse response = httpClient.execute(target, request, localContext)) {

                     // 401 if wrong user/password
                     System.out.println(response.getStatusLine().getStatusCode());

                     HttpEntity entity = response.getEntity();
                     if (entity != null) {
                         // return it as a String
                         String result = EntityUtils.toString(entity);
                         System.out.println(result.getBytes());
                         value.set(result);
                     }

                 }

             }
         });

      // Write the results to an attribute
         String results = value.get();
         if(results != null && !results.isEmpty()){
             flowFile = session.putAttribute(flowFile, "match", results);
         }

         // To write the results back out ot flow file
         flowFile = session.write(flowFile, new OutputStreamCallback() {

             @Override
             public void process(OutputStream out) throws IOException {
                 out.write(value.get().getBytes());
             }
         });

         session.transfer(flowFile, Success);

    }

}

Any help please ! It's been days and nothing seems right :(

Upvotes: 0

Views: 572

Answers (1)

Bryan Bende
Bryan Bende

Reputation: 18630

When implementing a source processor (meaning there is no input), then you want to use session.create() to create a brand new flow file. Using session.get() will attempt to get a flow file from incoming queues, but since there are none, it will always be null.

Upvotes: 1

Related Questions