All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.linmoure.utils.SenderUtils Maven / Gradle / Ivy

There is a newer version: 1.1.11
Show newest version
package io.github.linmoure.utils;

import com.alibaba.fastjson.JSONObject;
import io.github.linmoure.callback.GeneralCallback;
import io.github.linmoure.thread.ThreadPoolManager;
import io.github.linmoure.thread.runnable.LabelRunnable;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @author 李衡林
 */
@Slf4j
public class SenderUtils {


    private static Client CLIENT;

    private static Client getInstance() throws MalformedURLException {
        if (CLIENT == null) {
            synchronized (SenderUtils.class) {
                DynamicClientFactory factory = DynamicClientFactory.newInstance();
                factory.setAllowElementReferences(true);
                CLIENT = factory.createClient(new URL("https://gneral.haikewulian.com/gneral/Service/send?wsdl"));
            }
        }
        return CLIENT;
    }

    public static void sendGeneral(String method, JSONObject params) {
        Environment environment = SpringUtils.getBean(Environment.class);
        if ("prod".equals(environment.getProperty("spring.profiles.active"))) {
            String areaCode = System.getProperty("GENERAL_AREA_CODE");
            String placeCode = System.getProperty("GENERAL_PLACE_CODE");
            if (StringUtils.isEmpty(areaCode) || StringUtils.isEmpty(placeCode)) {
                throw new IllegalArgumentException("Please Configure GENERAL_AREA_CODE With GENERAL_PLACE_CODE");
            }
            params.put("areaCode", areaCode);
            params.put("placeCode", placeCode);
            params.put("sendTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            ThreadPoolManager.getDefaultExecutor().execute(new LabelRunnable() {
                @Override
                public String generateLabel() {
                    return "SEND_GENERAL";
                }

                @SneakyThrows
                @Override
                public void bnsRun() {
                    Object[] invoke = null;
                    String paramsString = params.toJSONString();
                    try {
                        log.info("params:{}", paramsString);
                        invoke = getInstance().invoke(method, new Object[]{paramsString});
                        log.info("send general result:{}", ArrayUtils.toString(invoke));
                    } catch (Exception e) {
                        log.error(e.getMessage());
                    } finally {
                        callback(method, params, invoke);
                    }
                }
            });
        }
    }

    private static void callback(String method, JSONObject params, Object[] invoke) {
        Map beansOfType = SpringUtils.getBeansOfType(GeneralCallback.class);
        if (!CollectionUtils.isEmpty(beansOfType)) {
            beansOfType.forEach((k, v) -> {
                v.callback(method, params, invoke);
            });
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy