Giriş
Şu satırı dahil ederiz
import com.hazelcast.client.config.ClientUserCodeDeploymentConfig;
Not : User Code Deployment hem client hem de member tarafından etkin olmalı. Yoksa çalışmaz.
Member tarafında kodla yapmak için UserCodeDeploymentConfig kullanılır
Member tarafındaki XML/YAML adımları şöyle
- Create declarative yaml file for members enabling user code deployment
- Create a StatefulSet manifest file that mounts a volume
- In the StatefulSet manifest, point to the path of the volume in the Java Opts
- Apply StatefulSet manifest file to cluster
- Copy yaml file into mounted volume
- Apply hazelcast image to cluster
- Hazelcast will now use the config previously stored in the volume and user code deployment will be enabled
Neden Lazım
Açıklaması şöyle
You can also deploy your code from the client side for the following situations:
1. You have objects that run on the cluster via the clients such as Runnable, Callable and EntryProcessor.
2. You have new user domain objects which need to be deployed into the cluster.
Mesela client tarafında kodlanmış bir EntryProcessor olsun. Ama bu EntryProcessor member tarafında çalışacak dolayısıyla member'a gönderilmesi lazım.
Örnek
Şöyle yaparız. Burada EntryProcessor aynı zamanda Serializable
public static class MyEntryProcessor extends AbstractEntryProcessor<String, Integer> implements Serializable { @Override public Object process(Map.Entry<String, Integer> entry) { ... } } public static void main(String[] args) { ClientConfig config = new ClientConfig(); ClientUserCodeDeploymentConfig ucd = config.getUserCodeDeploymentConfig(); ucd.setEnabled(true); ucd.addClass(MyEntryProcessor.class); HazelcastInstance hz = HazelcastClient.newHazelcastClient(config); IMap<String, Integer> map = hz.getMap("my-distributed-map"); map.put("key", 0); map.executeOnKey("key", new MyEntryProcessor()); ... hz.shutdown(); }
addClass metodu
Örnek
Şöyle yaparız
ClientConfig clientConfig = new ClientConfig(); ClientUserCodeDeploymentConfig ucd = new ClientUserCodeDeploymentConfig(); ucd.addClass("com.mycompany.task.ScheduledTask"); ucd.setEnabled(true); clientConfig.setUserCodeDeploymentConfig(ucd);
Örnek - EntryProcessor
Şöyle yaparız
ClientUserCodeDeploymentConfig ucd = new ClientUserCodeDeploymentConfig(); ucd.addClass(Person.class); ucd.addClass(PersonProcessor.class); ucd.setEnabled(true); clientConfig.setUserCodeDeploymentConfig(clientUserCodeDeploymentConfig);
setClassNames metodu - List
Örnek
Şöyle yaparız
ClientConfig clientConfig = new ClientConfig();ClientUserCodeDeploymentConfig distCLConfig = clientConfig.getUserCodeDeploymentConfig();List<String> classNames = new ArrayList<>();classNames.add("com.varane.models.Student");distCLConfig.setEnabled(true).setClassNames(classNames);return HazelcastClient.newHazelcastClient(clientConfig);
Böylece artık şöyle kullanabiliriz.
package com.varane.models; @Entity public class Student implements Serializable { @Id Integer id; ... } @Autowired private HazelcastInstance hazelcastInstance; @GetMapping("/sql-test") void sqlTest(){ SqlService sqlService = hazelcastInstance.getSql(); sqlService.execute("SELECT * FROM student"); }
Hiç yorum yok:
Yorum Gönder