Hazelcast 3.6 - Map get element - hazelcast

I have tried to make a simple demo using hazelcast 3.6 version. Basically I started the console application , then I added an element to the cluster from a client code.
public class GettingStartedClient {
public static void main(String[] args) {
ClientConfig clientConfig = new ClientConfig();
clientConfig.addAddress("127.0.0.1:5704");
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<Integer, String> map = client.getMap("customers");
System.out.println("Map Size:" + map.size());
for (int i = 0; i < map.size(); i++) {
System.out.println(map.get(i+1));
}
map.put(0, "Emre");
}
}
Later, Using the command line I switch to ns customer and I execute m.get , m.values and m.keys commands. m.values and m.keys are returning the element in the map however m.get returns null.
hazelcast[customers] > m.keys
0
Total 1
hazelcast[customers] > m.values
Emre
Total 1
hazelcast[customers] > m.get 0
null
Am I missing someting ? Any help is appreciated.

The key used in the Demo App is not Integer but String. So in order to make it work, your client code should be like this :
public static void main(String[] args) {
ClientConfig clientConfig = new ClientConfig();
clientConfig.addAddress("127.0.0.1:5704");
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<String, String> map = client.getMap("customers");
System.out.println("Map Size:" + map.size());
for (int i = 0; i < map.size(); i++) {
System.out.println(map.get(i+1));
}
map.put("0", "Emre");
}
You can checkout the source code of the demo app from github :
https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/console/ConsoleApp.java

Hazelcast console assume the key to be a string, and tries to find the key "0" which doesn't exist

Related

How is the parallel runner running the feature files

I have set the following code in my java file:
#CucumberOptions(tags = {"~#ignore"})
public class ExamplesTest {
#BeforeClass
public static void before() {
System.setProperty("karate.env", "dev");
}
#Test
public void testParallel() {
String karateOutputPath = "target/surefire-reports";
KarateStats stats = CucumberRunner.parallel(getClass(), 1, karateOutputPath);
generateReport(karateOutputPath);
assertTrue("there are scenario failures", stats.getFailCount() == 0);
}
public static void generateReport(String karateOutputPath) {
Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
List<String> jsonPaths = new ArrayList(jsonFiles.size());
jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
Configuration config = new Configuration(new File("target"), "demo");
ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
reportBuilder.generateReports();
}
}
As you can see, i have set the thread count to 1 , but even if i increase it i see no difference in the execution time.
I am not very sure how the parallel run is happening.
Can someone please explain.
Currently the unit of parallelization is at the feature file level. This means:
if you have 1 feature there is no effect
if you have multiple features but one of them takes a very long time, the test will run for that amount of time

Issue getting index field from hazelcast using predicate

I am trying to match a field that is not a key with a remote hazelcast, the goal here is to create many remote instances and use it to store serialized objects.
what i noticed is that if i do both put and SQL in the same run, the return works, as follow :
my class
public class Provider implements Serializable {
private String resourceId;
private String first;
public String getResourceId() {
return resourceId;
}
public void setResourceId(String resourceId) {
this.resourceId = resourceId;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
}
code :
/*********** map initlization ************/
Config config = new Config();
config.getNetworkConfig().setPublicAddress(host + ":" + port);
instance = Hazelcast.newHazelcastInstance(config);
map = instance.getMap("providerWithIndex");
String first = "XXXX"
/***** adding item ***************/
Provider provider = new Provider();
provider.setResourceId("11111");
provider.setFirst( first);
map.put(provider);
/********** getting items ************/
EntryObject e = new PredicateBuilder().getEntryObject();
Predicate predicate = e.get( "first" ).equal( first ) ;
Collection<Provider> providers = map.values(predicate);
once i run both put and get in different runs, the result is 0 - so to the same code i get no response.
my only thought is that it only does local fetch and not remote. but i hope i have a bug somewhere.
Your code looks good, apart from the fact that you can't do map.put(provider), you need to pass a key, so like map.put(someKey, provider);
The query looks & works fine.

Is there a way to generate a XML binding file from a class using MOXy?

I'm would like to use MOXy to marshal / unmarshal object from existing classes.
I would like to know if there is a mean to generate XML binding files (cause I don't want to use annotations) from my classes.
Or do we have to do it all with our little hands :) ?
By default JAXB/MOXy doesn't require any metadata to be specified (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html). You only need to specify the metadata where you want to override the default behaviour.
I'm guessing your real question is what is the easiest way to create the MOXy external mapping document. I do the following with Eclipse, there are probably similar steps for your favourite IDE:
Get the XML Schema for MOXy's mapping document
<EclipseLink_Home>/xsds/eclipselink_oxm_2_5.xsd
Register the XML Schema with your IDE
Eclipse | Preferences | XML | XML Catalog | Add
Create and XML document in the IDE and specify the following as the root element.
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"/>
Use the auto-complete functionality offered by your IDE to construct the XML document.
Another option is to generate jaxb classes and from those read the bindings (annotations) producing an external mapping (after which you can remove the annotations). PoC code:
public class MoxyBindingGenerator {
private static final String PACKAGE = "com.company.binding.jaxbclasses";
private static ObjectFactory xmlBindingsFactory = new ObjectFactory();
public static void main(String[] args) throws Exception {
Collection<TypeInfo> typeInfos = readAnnotations();
XmlBindings xmlBindings = xmlBindingsFactory.createXmlBindings();
xmlBindings.setPackageName(PACKAGE);
JavaTypes javaTypes = xmlBindingsFactory.createXmlBindingsJavaTypes();
xmlBindings.setJavaTypes(javaTypes);
List<JavaType> javaTypesList = javaTypes.getJavaType();
XmlEnums xmlEnums = xmlBindingsFactory.createXmlBindingsXmlEnums();
xmlBindings.setXmlEnums(xmlEnums);
List<XmlEnum> xmlEnumsList = xmlEnums.getXmlEnum();
typeInfos.stream().forEach(typeInfo -> {
if (!typeInfo.isEnumerationType()) {
fillJavaTypes(javaTypesList, typeInfo);
}
else {
fillEnumTypes(xmlEnumsList, typeInfo);
}
});
saveToFile(xmlBindings);
}
private static Collection<TypeInfo> readAnnotations() throws JAXBException, Exception {
JAXBContext jaxbContext = (JAXBContext) javax.xml.bind.JAXBContext.newInstance(PACKAGE);
Object contextState = getPrivateField(jaxbContext, "contextState");
Generator generator = (Generator) getPrivateField(contextState, "generator");
AnnotationsProcessor annotationsProcessor = generator.getAnnotationsProcessor();
Collection<TypeInfo> typeInfos = annotationsProcessor.getTypeInfo().values();
return typeInfos;
}
private static void fillEnumTypes(List<XmlEnum> xmlEnumsList, TypeInfo typeInfo) {
EnumTypeInfo et = (EnumTypeInfo) typeInfo;
XmlEnum xmlEnum = xmlBindingsFactory.createXmlEnum();
xmlEnum.setJavaEnum(et.getJavaClassName());
List<String> xmlEnumNames = et.getFieldNames();
List<Object> xmlEnumValues = et.getXmlEnumValues();
for (int i = 0; i < xmlEnumNames.size(); i++) {
String xmlEnumName = xmlEnumNames.get(i);
Object xmlEnumObject = xmlEnumValues.get(i);
XmlEnumValue xmlEnumValue = xmlBindingsFactory.createXmlEnumValue();
xmlEnumValue.setJavaEnumValue(xmlEnumName);
xmlEnumValue.setValue(xmlEnumObject.toString());
xmlEnum.getXmlEnumValue().add(xmlEnumValue);
}
xmlEnumsList.add(xmlEnum);
}
private static void fillJavaTypes(List<JavaType> javaTypesList, TypeInfo typeInfo) {
JavaType javaType = xmlBindingsFactory.createJavaType();
javaType.setName(typeInfo.getJavaClassName());
fillXmlType(javaType, typeInfo);
if (typeInfo.getXmlRootElement() != null) {
XmlRootElement xmlRootElement = typeInfo.getXmlRootElement();
xmlRootElement.setNamespace(null);
javaType.setXmlRootElement(xmlRootElement);
}
JavaAttributes javaAttributes = xmlBindingsFactory.createJavaTypeJavaAttributes();
javaType.setJavaAttributes(javaAttributes);
List<JAXBElement<? extends JavaAttribute>> javaAttributeList = javaAttributes.getJavaAttribute();
typeInfo.getNonTransientPropertiesInPropOrder().stream().forEach(field -> {
fillFields(javaAttributeList, field);
});
javaTypesList.add(javaType);
}
private static void fillFields(List<JAXBElement<? extends JavaAttribute>> javaAttributeList, Property field) {
if (field.getXmlElements() != null && field.getXmlElements().getXmlElement().size() > 0) {
XmlElements xmlElements = xmlBindingsFactory.createXmlElements();
xmlElements.setJavaAttribute(field.getPropertyName());
List<XmlElement> elements = field.getXmlElements().getXmlElement();
elements.stream().forEach(e -> {
e.setDefaultValue(null);
e.setNamespace(null);
xmlElements.getXmlElement().add(e);
});
JAXBElement<XmlElements> value = xmlBindingsFactory.createXmlElements(xmlElements);
javaAttributeList.add(value);
}
else if (!field.isAttribute()) {
XmlElement value = xmlBindingsFactory.createXmlElement();
value.setJavaAttribute(field.getPropertyName());
value.setName(field.getSchemaName().getLocalPart());
if (field.isNillable())
value.setNillable(field.isNillable());
if (field.isRequired())
value.setRequired(field.isRequired());
javaAttributeList.add(xmlBindingsFactory.createXmlElement(value));
}
else {
XmlAttribute value = xmlBindingsFactory.createXmlAttribute();
value.setJavaAttribute(field.getPropertyName());
value.setName(field.getSchemaName().getLocalPart());
javaAttributeList.add(xmlBindingsFactory.createXmlAttribute(value));
}
}
private static void saveToFile(XmlBindings xmlBindings)
throws JAXBException, PropertyException, FileNotFoundException, IOException {
JAXBContext xmlModelJaxbContext =
(JAXBContext) javax.xml.bind.JAXBContext.newInstance("org.eclipse.persistence.jaxb.xmlmodel");
JAXBMarshaller marshaller = xmlModelJaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
FileOutputStream fos = new FileOutputStream(new File(System.getProperty("user.home"), "binding-imspoor-oxm.xml"));
marshaller.marshal(xmlBindings, fos);
fos.close();
}
private static void fillXmlType(JavaType javaType, TypeInfo typeInfo) {
XmlType orgXmlType = typeInfo.getXmlType();
if (orgXmlType != null) {
boolean add = false;
XmlType xmlType = xmlBindingsFactory.createXmlType();
if (!StringUtils.isEmpty(orgXmlType.getName())) {
xmlType.setName(orgXmlType.getName());
add = true;
}
if (orgXmlType.getPropOrder() != null && orgXmlType.getPropOrder().size() > 1) {
xmlType.getPropOrder().addAll(orgXmlType.getPropOrder());
add = true;
}
if (add)
javaType.setXmlType(xmlType);
}
}
private static Object getPrivateField(Object obj, String fieldName) throws Exception {
Field declaredField = obj.getClass().getDeclaredField(fieldName);
declaredField.setAccessible(true);
return declaredField.get(obj);
}
}

Hazelcast Near Cache

I have a setup of two node hazelcast 3.2 with each node containing 500 MB of data.I have configured client side Near cache.
I have following questions
How to verify that data is fetched from near cache hashmap?
Some of my maps have only one entry i.e., size=1, which sometimes on
heavy load takes 20 to 30 ms to fetch the data?
The time taken on heavy load is between 4ms to 20 ms.If it is
fetching from near cache,what will be the optimal fetch time in
millisecond.
What is the best GC policy to be set on the server?
My server side config
mapCfg = new MapConfig();
mapCfg.setName("TL_MAP_2");
mapCfg.setBackupCount(1);
mapCfg.setReadBackupData(true);//enabled as it is sync backup
mapCfg.setInMemoryFormat(InMemoryFormat.BINARY);
cfg.addMapConfig(mapCfg);
My client side code is a singleton
public enum CacheService {
INSTANCE;
private static ClientConfig clientConfig = null;
private static HazelcastInstance cacheClient = null;
private static Set<String> nearcaches = new HashSet<String>(Arrays.asList("TL_MAP_1","TL_MAP_2","TL_MAP_3","TL_MAP_4","TL_MAP_5","TL_MAP_6","TL_MAP_7","TL_MAP_8","TL_MAP_9","TL_MAP_10"));
static{
if(clientConfig ==null || cacheClient ==null){
clientConfig = new ClientConfig();
clientConfig.getGroupConfig().setName("dev").setPassword("dev-pass");
clientConfig.getNetworkConfig().addAddress("XXX.XX.XXX.XXX:5701","XXX.XX.XXX.XXX:5702").setSmartRouting(true);;
clientConfig.setExecutorPoolSize(120);
Iterator<String> iterator = nearcaches.iterator();
String near = null;
while(iterator.hasNext()){
near = iterator.next();
clientConfig.addNearCacheConfig(near, new NearCacheConfig().setInMemoryFormat(InMemoryFormat.OBJECT));
}
cacheClient = HazelcastClient.newHazelcastClient(clientConfig);
}
}
public HazelcastInstance getCacheClient(){
return cacheClient;
}
public Map get(String cacheName) {
Map cacheMap = cacheClient.getMap(cacheName);
if (nearcaches.contains(cacheName)) {
NearCacheStats stats = ((IMap) cacheMap).getLocalMapStats()
.getNearCacheStats();
logger.info("Near Cache HITS for map " + cacheName + " ::"
+ stats.getHits());
}
return cacheMap;
}
}

Inter process communication on same machine using tcp

I have two processes Client and server.
This are as follws :
This is my client process:-
[Serializable ]
public class retobj
{
public int a;
}
class client
{
static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5005));
Console.WriteLine("Connected.");
retobj ob = new retobj();
ob.a = 90;
BinaryFormatter bf = new BinaryFormatter();
NetworkStream ns = client.GetStream();
bf.Serialize(ns, ob);
Console.WriteLine("Data sent.");
Console.ReadLine();
ns.Close();
client.Close();
}
}
And this is my server process:
[Serializable]
public class retobj
{
public int a;
}
class server
{
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, 5005);
listener.Start();
Console.WriteLine("Server started.");
Socket client = listener.AcceptSocket();
Console.WriteLine("Accepted client {0}.\n", client.RemoteEndPoint);
List<string> l = null;
retobj j = null;
using (NetworkStream ns = new NetworkStream(client))
{
BinaryFormatter bf = new BinaryFormatter();
j = (retobj )bf.Deserialize(ns);
}
//if (l != null)
// foreach (var item in l)
// Console.WriteLine(item);
Console.WriteLine(j.a);
Console.ReadLine();
client.Close();
listener.Stop();
}
But it gives an error like:
Error in server process:
Unable to find assembly 'ConsoleApplication45, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
When you serialize an object using the BinaryFormatter, it includes information about what assembly the object came from. When deserializing it on the server, it reads that information and looks for the version of the retobj class from the client assembly, which is why you get that error. The one on the server is not the same.
Try moving that class to a Class Library project, and referencing that project from both the client and the server. You don't need two copies.
An alternative approach would be to use an alternate formatter, like the DataContractSerializer, that does not embed assembly information.

Resources