DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Automate Migration Assessment With XML Linter
  • Validate XML Request Against XML Schema in Mule 4
  • How To Get the Comments From a DOCX Document in Java
  • Exploring Hazelcast With Spring Boot

Trending

  • Understanding Git
  • Creating a Deep vs. Shallow Copy of an Object in Java
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Application Integration for IoT Devices: A Detailed Guide To Unifying Your Ecosystem
  1. DZone
  2. Coding
  3. Languages
  4. Using Apache Ignite.NET in LINQPad

Using Apache Ignite.NET in LINQPad

Here is a quick how-to for using Apache Ignite.NET in LINQPad.

Pavel Tupitsyn user avatar by
Pavel Tupitsyn
·
Aug. 19, 16 · Tutorial
Like (3)
Save
Tweet
Share
3.41K Views

Join the DZone community and get the full member experience.

Join For Free

LINQPad is a must-have tool for every .NET developer, and it is a great way to explore and try Ignite.NET APIs.

LINQPad Logo

Getting Started

  1. Download LINQPad: linqpad.net/Download.aspx. Make sure to choose AnyCPU version on x64 OS.
  2. Install Ignite.NET NuGet packages:
    • Press F4 (or click Query -> References and Properties menu).
    • Click “Add NuGet…”. A warning may appear: As you don't have LINQPad Premium/Developer Edition, you can only search for NuGet packages that include LINQPad samples. This is fine since Ignite packages do include LINQPad samples.
    • Install packages by clicking “Add To Query” button.
    • Click “Add namespaces” link button and add (at least) the first one: Apache.Ignite.Core.
    • Close NuGet window, click OK on Query Properties window.
  3. Make sure Language drop-down is set to “C# Expression” (this is the default).
  4. Type Ignition.Start() (without semicolon) and hit F5.

Ignite node starts and you can see the usual console output in the output pane, as well as the resulting Ignite object dump.

Check out the bundled examples on “Samples” tab to the left.

Ignite in LINQPad

Recycling the Worker Process

LINQPad runs user code in a separate process. By default, this process is reused between runs (for performance reasons).

This has two consequences:

  1. Ignite.NET starts an in-process JVM. This JVM is reused when the process is reused, so you can’t change the JVM options.
  2. Ignition class keeps all started nodes in a static map. These nodes will keep running when the process is reused, which may cause Default Ignite instance has already been started exception if you run Ignition.Start() code twice.

This behavior may be useful in some scenarios and unwanted in other, but the best thing is that we can control it via built-in Util.NewProcess property. Switch Language dropdown on top to “C# Statement(s)” mode and run the following script:

Util.NewProcess = true;
Ignition.Start();

This script can be run multiple times with no issues, since every time we start from scratch.

Reusing Started Node

Ignite node takes some time to start (8 seconds on my machine) due to JVM startup and network discovery process. To quickly iterate on our code in LINQPad, we can reuse started node between runs. For example, the following code reuses started Ignite instance and reuses existing cache, adding one item per run and showing existing items:

// Get existing instance or start a new one
var ignite = Ignition.TryGetIgnite() ?? Ignition.Start();

// Get existing cache or create a new one
var cache = ignite.GetOrCreateCache<Guid, DateTime>("cache");

// Add a new entry
cache[Guid.NewGuid()] = DateTime.Now;

// Show all entries
cache.Dump();

Contrary to several seconds for a fresh node start, this code runs in a couple of milliseconds.

When needed, use Shift+Control+F5 to unload the AppDomain and start from scratch.

Usage Tips and Tricks

Besides exploring the Ignite API in general, I’ve come up with the following Ignite+LINQPad use cases:

Examine Cache in a Running Cluster

Visor command line can show the cache contents, but doing this in LINQPad is much more flexible and user-friendly. Since we don’t have any actual classes in the LINQPad script, we have to use cache in binary mode in order to be able to read the contents.

The following code shows a list of all caches along with top 5 cache entries from each of them:

var ignite = Ignition.TryGetIgnite() ?? Ignition.Start();

foreach (var cacheName in ignite.GetCacheNames())
    ignite.GetCache<object, object>(cacheName)
        .WithKeepBinary<object, object>()
        .Select(x => x.ToString())
        .Take(5)
        .Dump(cacheName);

Convert Spring XML Configuration to C# IgniteConfiguration

Let’s say you have some Ignite Sring XML configuration file and need the same configuration for Ignite.NET; or you are migrating from Ignite.NET 1.5, where Spring XML was the only configuration mechanism.

You can simply start a node with said Spring XML file and call GetConfiguration() to see how it looks in .NET:

Ignition.Start(@"spring-config.xml").GetConfiguration()

Convert IgniteConfiguration to app.config XML

Ignite.NET supports app.config and web.config configuration. However, writing XML is no fun. UsingIgniteConfiguration in C# is easier, IDE helps a lot, and invalid code just won’t compile.

To help us with XML, there is a hidden way to convert IgniteConfiguration instance to the XML representation. The following snippet shows how to use it via Reflection (make sure to set Language dropdown to C# Program):

void Main()
{
    new IgniteConfiguration
    {
        CacheConfiguration = new[]
        {
            new CacheConfiguration
            {
                Name = "myCache",
                CacheMode = CacheMode.Replicated
            }
        }
    }.ToXml().Dump();
}

public static class IgniteConfigurationExtensions
{
    public static string ToXml(this IgniteConfiguration cfg)
    {
        var sb = new StringBuilder();

        var settings = new XmlWriterSettings
        {
            Indent = true
        };

        using (var xmlWriter = XmlWriter.Create(sb, settings))
        {
            typeof(Ignition).Assembly
                .GetType("Apache.Ignite.Core.Impl.Common.IgniteConfigurationXmlSerializer")
                .GetMethod("Serialize")
                .Invoke(null, new object[] {cfg, xmlWriter, "igniteConfiguration"});
        }

        return sb.ToString();
    }
}

And the result is:

<?xml version="1.0" encoding="utf-16"?>
<igniteConfiguration xmlns="http://ignite.apache.org/schema/dotnet/IgniteConfigurationSection">
    <cacheConfiguration>
        <cacheConfiguration name="myCache" cacheMode="Replicated" />
    </cacheConfiguration>
</igniteConfiguration>

Combined with the previous Spring XML use case, you can also convert Spring XML to app.config XML!

Next Ignite.NET versions will include this ToXml method as part of the public API, but for 1.6 and 1.7 Reflection is the way to go.

XML

Published at DZone with permission of Pavel Tupitsyn. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Automate Migration Assessment With XML Linter
  • Validate XML Request Against XML Schema in Mule 4
  • How To Get the Comments From a DOCX Document in Java
  • Exploring Hazelcast With Spring Boot

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: