BrowseCrawler — Offline crawling of entire trees
Overview
IDE / HMI When working online,BrowseCrawler it is often necessary to load the AddressSpace subtree locally. Use a queue BFS to traverse it, with double protection (maxDepth + maxNodes).
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| new BrowseCrawler(ua) | Structure | — | Default: maxDepth=10, maxNodes=100000, filter=Unspecified |
| new BrowseCrawler(ua, maxDepth, maxNodes, nodeClassFilter) | Structure | — | nodeClassFilter To Enums.NodeClass |
| crawlAsync(rootNodeId, progress, cancelToken) | Methods | Read | 3 ginseng; returns CompletableFuture<CrawlResult> |
| CrawlResult.allNodes() | Properties | Read | Flat Node List (List<CrawlNode>) |
| CrawlResult.childrenByParent() | Properties | Read | Father-Son Relationship Map |
| CrawlResult.elapsed() | Properties | Read | Total Duration |
crawlAsync has no 2-arg overload; the three formal parameters are String rootNodeId / BiConsumer<Integer, String> progress / java.util.concurrent.atomic.AtomicBoolean cancelToken.rootNodeId Passing null or an empty string = starts with WellKnownNodes.OBJECTS_FOLDER (i=85); Callback is triggered once every progress 100 nodes; null may be passed; When null is cancelToken passed, an irrevocable token is created internally.
Code Example
import com.darra.opcua.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();
// 1) Climb the entire tree ObjectsFolder
BrowseCrawler crawler = new BrowseCrawler(ua, 8, 50_000, Enums.NodeClass.Unspecified);
BiConsumer<Integer, String> progress = (count, nid) ->
System.out.printf("\r已抓 %d 个, 当前 %s", count, nid);
AtomicBoolean cancel1 = new AtomicBoolean(false);
CompletableFuture<CrawlResult> fut = crawler.crawlAsync(
WellKnownNodes.OBJECTS_FOLDER, progress, cancel1);
CrawlResult result = fut.get();
System.out.printf("%n共 %d 节点, 耗时 %.1fs%n",
result.allNodes().size(), result.elapsed().toMillis() / 1000.0);
// 2) Variable nodes only
BrowseCrawler c2 = new BrowseCrawler(ua, 10, 100_000, Enums.NodeClass.Variable);
CrawlResult r2 = c2.crawlAsync("ns=2;s=Boilers", null, new AtomicBoolean(false)).get();
for (CrawlNode n : r2.allNodes())
System.out.println(" " + n.browseName() + " (" + n.nodeId() + ")");
// 3) Remove support — set to cancelToken, not cancel() CompletableFuture
AtomicBoolean cancel3 = new AtomicBoolean(false);
CompletableFuture<CrawlResult> fut3 = crawler.crawlAsync("i=85", null, cancel3);
cancelButton.addActionListener(e -> cancel3.set(true));
}
Performance
Crawl time depends on the number of server nodes and the network round-trip time — at least one Browse RPC per node. The SDK has no benchmark figures; measure it on your own server. maxNodes is a hard limit; when it is reached, crawling stops.
Best Practices
- Restricted to root; do not start from i=84 (Root)
- Use nodeClassFilter to save memory
- Crawl once at startup; subsequently, look up from the local cache.
- Provide users with a "Cancel" button
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| BrowseCrawler | BrowseCrawler | BrowseCrawler | BrowseCrawler | BrowseCrawler | DarraUa_Session_CrawlAddressSpace |
The C-side entry point is a single function DarraUa_Session_CrawlAddressSpace(session, config, on_node, ctx), accompanied DarraUa_CrawlerConfig byDarraUa_CrawlerConfig_Init an initialization and DarraUa_CrawlNode a callback DarraUa_OnCrawlNode.