Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration problem of listeners #10154

Closed
stonecauldron opened this issue Mar 27, 2017 · 6 comments

Comments

Projects
None yet
2 participants
@stonecauldron
Copy link

commented Mar 27, 2017

Info

  • Hazelcast version : 3.7.2
  • Cluster size : 1
  • Number of clients: 1
  • Java version: 8
  • OS: Windows 10
  • Hibernate version: 5.1.1
  • Spring version: 4.3.2

Description of the problem

Setting up a Hazelcast listener in a Spring configuration format does not seem to work when using the class property of hz:listener. More specifically in the file hazelcast-context:

<hz:client ...>
...
<hz:listeners>
    <hz:listener implementation="<BeanID>"/>
...
</hz:client>
...
<bean class="<BeanClass>" id="<BeanClass>"/> 

This configuration successfully registers BeanClass as an event listener while the one below does not:

<hz:client ...>
...
<hz:listeners>
    <hz:listener class-name="<BeanID>"/>
...
</hz:client>

I don't understand why this is the case, the documentation seems to imply that this should be possible.

@jerrinot

This comment has been minimized.

Copy link
Contributor

commented Mar 27, 2017

@stonecauldron,

thanks for your report. Just to double-check - I presume you meant to write

<hz:listeners>
    <hz:listener class-name="<BeanClass>"/>
...
</hz:client>

is this correct?

@stonecauldron

This comment has been minimized.

Copy link
Author

commented Mar 27, 2017

Yes this is correct, my mistake.

@jerrinot jerrinot self-assigned this Mar 28, 2017

@jerrinot

This comment has been minimized.

Copy link
Contributor

commented Mar 28, 2017

@stonecauldron: what type of listener you have? can you share its source code?

@stonecauldron

This comment has been minimized.

Copy link
Author

commented Mar 28, 2017

I am using a LifecycleListener, you can find its source code below:

package com.company.project.helpers;

import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Scanner;

/**
 * This class provides a confirmation prompt when connecting to a hazelcast-cluster in the cloud
 * machines.
*/
@Service
public class HzConnectionGuardian implements LifecycleListener {

    @Value("${hazelcast.network.member}")
    private String hzHost;

    @Value("#{'${hazelcast.network.dangerousMembers}'.split(',')}")
    private List<String> dangerousHosts;

    private void checkConnection() {
        if (isConnectingToDangerousHost() && (!isConnectingToLocalHost())) {
            System.out.println("You are connecting to the hazelcast cluster in " + hzHost);
            System.out.println("Are you sure you want to continue? [y/n]");

            Scanner s = new Scanner(System.in);
            String userPrompt = s.nextLine().toLowerCase();
            if (!userPrompt.contains("y")) {
                System.out.println("Quitting...");
                System.exit(0);
            }
        }
    }

    private boolean isConnectingToDangerousHost() {
        return dangerousHosts.stream()
                .anyMatch(str -> hzHost.contains(str));
    }

    private boolean isConnectingToLocalHost() {
        try {
            int portStartIndex = hzHost.indexOf(":");
            String hostWithoutPort = hzHost.substring(0, portStartIndex);

            String localHostAddress = InetAddress.getLocalHost().getHostAddress();
            String hzClusterHostAddress = InetAddress.getByName(hostWithoutPort).getHostAddress();

            return localHostAddress.equals(hzClusterHostAddress);
        } catch (UnknownHostException e) {
            return false;
        }
    }

    @Override
    public void stateChanged(LifecycleEvent event) {
        checkConnection();
    }
}
@jerrinot

This comment has been minimized.

Copy link
Contributor

commented Mar 28, 2017

@stonecauldron: thanks for a very nice bug report. I can confirm it's indeed a bug in a client implementation. I sent a patch and it should be backportable into Hazelcast 3.8.2 release.

@stonecauldron

This comment has been minimized.

Copy link
Author

commented Mar 28, 2017

@jerrinot thank you for taking the time to look into it and correcting it so quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.