Hot questions for Using Azure in networking

Top Java Programmings / Azure / networking


I am not sure whether this question is Mina-related or more Azure-related but it has to do with the networking. I have also added Netty tag since Mina and Netty share many networking principles.

I hope to get an advice where to dig into.

I have used certain Mina application quite long in local network, now I am trying to migrate it into the cloud. I deploy Linux virtual machines in Azure (each has public IP but does this really matter?).

They connect (using Mina) to a machine outside Azure that also has its own public IP. Usual thing:

SocketConnector connector = new NioSocketConnector(numberOfConnectors);
ConnectFuture connectFuture = connector.connect(new
InetSocketAddress(remoteHost, remotePort));

That Mina machine outside the Azure also runs Mina. Let's call it server machine.

It accepts connections like this:

NioSocketAcceptor acceptor = new NioSocketAcceptor(acceptor_threads);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, iddle_time);
acceptor.getFilterChain().addLast("codec", new
acceptor.setDefaultLocalAddress(new InetSocketAddress(port));

When Azure applications connect to server machine, server saves

IoSession session

to asynchronously push messages back in future like this:


This worked well inside a local network (without Azure), but in current deployment server sends message

2017-01-17/15:45:19.823/GMT-00:00 [nioEventLoopGroup-3-3] [...] DEBUG
Sending message to /

and an Azure machine does not receive anything. Moreover, after a while the following exception arises on server machine:

2017-01-17/16:01:11.419/GMT-00:00 [NioProcessor-4] [...] ERROR
Exception in IOHandlerConnection timed out Connection timed out
    at Method)
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.access$600(
    at org.apache.mina.core.polling.AbstractPollingIoProcessor$
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$

2017-01-17/16:01:11.424/GMT-00:00 [NioProcessor-3] [...] DEBUG sessionClosed

I use Mina version 2.0.4 (yes, it is old but it works on local network for several years for now).

I setup Azure network with Java Azure SDK 1.0.0-beta3

Network.DefinitionStages.WithCreate creatableNetwork = azure.networks()

And create virtual machines as

VirtualMachine.DefinitionStages.WithCreate creatableVirtualMachine =
           .withNewPrimaryPublicIpAddress(String.format("chr-vm-%04d", i))                  .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)

I wonder what reasons may prevent traveling messages from server to Azure client machines? Azure network configuration? Mina configuration? (the first messages from client machines to server machine do come after they connect)

I hope that above information may contain a clue.


I have solved my problem thanks to Peter Pan - MSFT noting about NSG - Network Security Group.

NSG controls in/out rules like a Windows Firewall. You should create NSG, add rules to it, and assign NSG to a particular entity:

There are at least two options to assign NSG:

  • to a network subnet
  • to a network interface

There is a tutorial 1 and Java code sample 2. In my case, a separate network interface is created for each VM (since each VM has public IP). So, I assigned one NSG to a single subnet.

Fisrt, create NSG:

NetworkSecurityGroup NSG = azure.networkSecurityGroups()
                        .withDescription("Incoming messsages")

Than modify the code to explicitly define a subnet and assign NSG to it ( subnet1 is automatically created without NSG if none defined explicitly)

Network.DefinitionStages.WithCreate creatableNetwork = azure.networks()

So, the rest of the code remains the same as posted in the question above.

Helpful links:

  1. Azure Portal Tutorial
  2. Java Azure SDK NSG Example


I can't find the Java API from the Azure SDK to manage PostgreSQL firewall rules.

The REST API is documented here.

Does anyone know which Maven dependency to use?

Thanks, Chris


Not sure if this is what you're looking for but you can find source code for Java SDK for PostgreSQL here:


I can't find the Java API from the Azure SDK to delete a NetworkSecurityRule resource.

The REST API is documented here.

I use this Maven dependency:

In my code I hold a reference to a NetworkManager instance and I have a collection of NetworkSecurityRule objects.

Does anyone know how to do it?

Thanks, Chris


According to my test, we can use the following code. For more details, please refer to the docuemnt 1. create a service principal and assign Reader role for the sp.

az login
az account set --subscription "<your subscription id>"
# it will assign  Contributor to the sp at subscription level
az ad sp create-for-rbac -n "mysample" --role  Contributor

  1. code
public static void main(String[] args){
 String clientId = "your sp appId";
       String secret = "your sp password";
       String domain = "your tenant domain";
       ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret,
       Azure azure = AzureAzure.configure().withLogLevel(LogLevel.BASIC).authenticate(credentials)

       NetworkSecurityGroup group = azure.networkSecurityGroups().getById(
            "your nsg resource id");

       for(String i : group.securityRules().keySet()){



       group = azure.networkSecurityGroups().getById(
       for(String i : group.securityRules().keySet()){