Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

OPC NiFi processor generate error java.net.SocketException: Protocol family unavailable

OPC NiFi processor generate error java.net.SocketException: Protocol family unavailable

New Contributor

Hello,

I am making a new NiFi processor that supports OPC DA connectivity (A protocol that is used for retrieving data from some machines).

The code is running successfully in eclipse (using main method), but when I export to a nar file and run it in nifi it generates the following error in the log file

org.jinterop.dcom.common.JIException: An internal error occurred. [0x8001FFFF]
	at org.jinterop.dcom.core.JIRemUnknownServer.call(JIRemUnknownServer.java:173)
	at org.jinterop.dcom.core.JIRemUnknownServer.addRef_ReleaseRef(JIRemUnknownServer.java:199)
	at org.jinterop.dcom.core.JIComObjectImpl.addRef(JIComObjectImpl.java:123)
	at org.jinterop.dcom.core.JIComServer.createInstance(JIComServer.java:876)
	at org.openscada.opc.lib.da.Server.connect(Server.java:121)
	at com.ibm.ocp.nifi.processor.GetOPCDAData.onTrigger(GetOPCDAData.java:130)
	at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
	at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1122)
	at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:147)
	at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:47)
	at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:128)
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Protocol family unavailable
	at sun.nio.ch.Net.connect0(Native Method)
	at sun.nio.ch.Net.connect(Unknown Source)
	at sun.nio.ch.Net.connect(Unknown Source)
	at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
	at java.nio.channels.SocketChannel.open(Unknown Source)
	at org.jinterop.dcom.transport.JIComTransport.attach(JIComTransport.java:119)
	at rpc.Stub.attach(Stub.java:126)
	at org.jinterop.dcom.core.JIRemUnknownServer.call(JIRemUnknownServer.java:154)
	... 17 more

This is the nifi processor code

package com.ibm.ocp.nifi.processor;


import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;


import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.ProcessorInitializationContext;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.OutputStreamCallback;
import org.apache.nifi.processor.util.StandardValidators;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
import org.openscada.opc.lib.da.ItemState;
import org.openscada.opc.lib.da.Server;




@Tags({"OPC", "DA"})
@CapabilityDescription("Fetch data from OPC Server using DA protocol")
public class GetOPCDAData extends AbstractProcessor {


	private List<PropertyDescriptor> properties;
	private Set<Relationship> relationships;
	
	public static final String MATCH_ATTR = "match";


	public static final PropertyDescriptor HOSTNAME = new PropertyDescriptor.Builder()
	        .name("OPC Server hostname")
	        .required(true)
	        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
	        .build();
	public static final PropertyDescriptor DOMAIN = new PropertyDescriptor.Builder()
	        .name("Domain")
	        .required(false)
	        .build();
	public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder()
	        .name("Username")
	        .required(true)
	        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
	        .build();
	public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder()
	        .name("Password").sensitive(true)
	        .required(true)
	        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
	        .build();
	public static final PropertyDescriptor CLSID = new PropertyDescriptor.Builder()
	        .name("CLSID")
	        .required(false)
	        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
	        .description("Either enter CLSID or ProgId")
	        .build();
	public static final PropertyDescriptor PROGID = new PropertyDescriptor.Builder()
	        .name("Prog ID")
	        .required(false)
	        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
	        .description("Either enter CLSID or ProgId")
	        .build();


	public static final Relationship SUCCESS = new Relationship.Builder()
	        .name("SUCCESS")
	        .description("Succes relationship")
	        .build();
	
	
	@Override
	public void init(final ProcessorInitializationContext context){
	    List<PropertyDescriptor> properties = new ArrayList<>();
	    properties.add(HOSTNAME);
	    properties.add(DOMAIN);
	    properties.add(USERNAME);
	    properties.add(PASSWORD);
	    properties.add(CLSID);
	    properties.add(PROGID);
	    this.properties = Collections.unmodifiableList(properties);


	    Set<Relationship> relationships = new HashSet<>();
	    relationships.add(SUCCESS);
	    this.relationships = Collections.unmodifiableSet(relationships);
	}


	@Override
	public Set<Relationship> getRelationships(){
	    return relationships;
	}


	@Override
	public List<PropertyDescriptor> getSupportedPropertyDescriptors(){
	    return properties;
	}
	@Override
	public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
		// TODO Auto-generated method stub
		final ConnectionInformation ci = new ConnectionInformation();
/*
		ci.setHost(context.getProperty(HOSTNAME).getValue());
		ci.setUser(context.getProperty(USERNAME).getValue());		
		ci.setPassword(context.getProperty(PASSWORD).getValue());
		
		String domain = context.getProperty(DOMAIN).getValue();
		String clsid = context.getProperty(CLSID).getValue();
		String progId = context.getProperty(PROGID).getValue();
		if (domain != null && !domain.isEmpty())
			ci.setDomain(domain);
		if (clsid != null && !clsid.isEmpty())
			ci.setClsid(clsid);
		if (progId != null && !progId.isEmpty())
			ci.setProgId(progId);
*/		
		ci.setHost("localhost");
		ci.setUser("Alzobair");
		ci.setPassword("*******");
		ci.setClsid("ACB049F7-8529-48CC-814F-0D63635A36AA");
		Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
		try {
			// connect to server
			server.connect();
			server.setDefaultUpdateRate(2000);
			server.addGroup("MyGroup");
			Group group = server.findGroup("MyGroup");
			Item item = group.addItem("Random.Int1");
			ItemState itemState = item.read(false); 
			String s = item.getId() + "," 
					+ itemState.getValue().toString() 
					+ "," + itemState.getQuality()
					+ (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(itemState.getTimestamp().getTime());
			
			FlowFile flowFile = session.get();
			flowFile = session.write(flowFile, new OutputStreamCallback() {


	            @Override
	            public void process(OutputStream out) throws IOException {
	                out.write(s.getBytes());
	            }
	        });
			session.transfer(flowFile, SUCCESS); 


		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args)
	{
		ProcessContext context = null;
		ProcessSession session = null;
		(new GetOPCDAData()).onTrigger(context,session);
	}
	
}


Don't have an account?
Coming from Hortonworks? Activate your account here