3

我们可以删除特定区域中的整个数据,但是如果可以,是否可以删除一个特定的键及其在 gemfire 中的值,那么如何? 如果没有,那为什么

GeodeConfiguration 类

@Configuration 公共类 GeodeConfiguration {

@SuppressWarnings("rawtypes")
@Bean
Region gemfireRegion() throws InterruptedException {

    ClientCache cache = new ClientCacheFactory()
            .setPdxSerializer(
                    new ReflectionBasedAutoSerializer(PropertiesCache
                            .getInstance()
                            .getProperty("pdxSerializerValue")))
            .addPoolLocator(
                    PropertiesCache.getInstance().getProperty("hostname"),
                    10334).create();
    Region<String, String> region = cache
            .<String, String> createClientRegionFactory(
                    ClientRegionShortcut.CACHING_PROXY)

            .setEntryTimeToLive(new ExpirationAttributes(50))
            .create(PropertiesCache.getInstance().getProperty("region"));

    return region;

}

}

移动控制器类

@Controller 公共类 MobileController {

@Autowired
Operations operations;

Msg msg = new Msg();
final static Logger logger = Logger.getLogger(MobileController.class);

@RequestMapping(value = "/mobile", method = RequestMethod.POST)
public void dataFromEs(@RequestParam("name") String name,
        @RequestParam("message") String message,
        @RequestParam("mobileNo") String[] mobileNo) throws IOException,
        JSONException {

    JSONObject obj = new JSONObject();

    obj.put("userName", name);
    obj.put("message", message);

    JSONObject mainObj = new JSONObject();

    JSONArray jsonArray = new JSONArray();

    for (int counter = 0; counter < mobileNo.length; counter++) {
        // 1st object

        JSONObject list1 = new JSONObject();
        list1.put("mobilenumber", mobileNo[counter]);

        jsonArray.put(list1);
    }
    mainObj.put("data", obj);

    obj.put("mobileno", jsonArray);
    logger.info("jsonString----------->: " + jsonArray);

    logger.info(mainObj);

    msg.setKey(name);
    msg.setValue(mainObj.toString());

    operations.saveMessageData(msg);

    sendMSG(name);

}

public void sendMSG(String name) throws ClientProtocolException,
        IOException, JSONException {

    String objMobileNo;
    String objMessage;
    String objUserName;
    String userStatus;

    JSONObject root = new JSONObject(operations.getDataValue(name));

    JSONObject jsonObject = root.getJSONObject("data");

    objUserName = (String) jsonObject.get("userName");
    objMessage = (String) jsonObject.get("message");

    logger.info("UserName ----> " + objUserName);
    logger.info("Message sent ---> " + objMessage);

    JSONArray mobArray = jsonObject.getJSONArray("mobileno");

    // now get the first element:
    for (int count = 0; count < mobArray.length(); count++) {
        objMobileNo = (String) mobArray.getJSONObject(count).get(
                "mobilenumber");

        logger.info("Mobile number ----> " + objMobileNo);

        JSONObject obj = new JSONObject();
        JSONObject objDraftOne = new JSONObject();
        JSONObject objDraftTwo = new JSONObject();
        JSONObject jsonObjectToSave = new JSONObject();
        JSONObject jsonObjectToSaveDraft = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        objDraftTwo.put("mobilenumber", objMobileNo);
        jsonArray.put(objDraftTwo);
        objDraftOne.put("mobileno", jsonArray);
        objDraftOne.put("userName", objUserName);
        objDraftOne.put("message", objMessage);
        jsonObjectToSaveDraft.put("data", objDraftOne);
        obj.put("userName", objUserName);
        obj.put("message", objMessage);
        obj.put("mobilenumber", objMobileNo);
        jsonObjectToSave.put("data", obj);
        HttpClient client = HttpClientBuilder.create().build();

        HttpPost request = new HttpPost(
                "https://xyzotp.bdt.xyz.com/sendSMS");
        StringEntity params = new StringEntity(jsonObjectToSave.toString());
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = client.execute(request);
        String responseAsString = EntityUtils
                .toString(response.getEntity());

        JSONObject responseObjectStatus = new JSONObject(responseAsString);

        userStatus = (String) responseObjectStatus.get("message");

        logger.info("Message Status: " + userStatus);

        if (!userStatus.equals("Message sent successfully")) {
            logger.info(jsonObjectToSaveDraft);

        }

    }

}

}

在上面的代码中,名称是每次都会更改的,我们要删除键及其传递的值

4

1 回答 1

1

在一个区域上,有一种用于删除 key 和 value 的 remove 方法region.remove(key)。有关详细信息,请阅读javadocs

于 2016-08-22T19:04:40.150 回答