Java Code Examples for org.fisco.bcos.channel.client.Service#run()

The following examples show how to use org.fisco.bcos.channel.client.Service#run() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBase.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  // 获取spring配置文件,生成上下文
  context = new ClassPathXmlApplicationContext("applicationContext.xml");
  //   ((ClassPathXmlApplicationContext) context).start();

  Service service = context.getBean(Service.class);
  service.run();

  System.out.println("start...");
  System.out.println("===================================================================");

  ChannelEthereumService channelEthereumService = new ChannelEthereumService();
  channelEthereumService.setChannelService(service);

  web3j = Web3j.build(channelEthereumService, service.getGroupId());
  // EthBlockNumber ethBlockNumber = web3.ethBlockNumber().send();

  Ok ok = Ok.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
  address = ok.getContractAddress();
  blockNumber = ok.getTransactionReceipt().get().getBlockNumber();
  blockHash = ok.getTransactionReceipt().get().getBlockHash();
  txHash = ok.getTransactionReceipt().get().getTransactionHash();
}
 
Example 2
Source File: TestBase.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {

	context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

  Service service = context.getBean(Service.class);
  service.run();

  ChannelEthereumService channelEthereumService = new ChannelEthereumService();
  channelEthereumService.setChannelService(service);

  System.out.println("EncryptType =>  " + EncryptType.getEncryptType());

  web3j = Web3j.build(channelEthereumService, service.getGroupId());
  credentials = GenCredential.create();
  Ok ok = Ok.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
  blockNumber = ok.getTransactionReceipt().get().getBlockNumber();
  blockHash = ok.getTransactionReceipt().get().getBlockHash();
  txHash = ok.getTransactionReceipt().get().getTransactionHash();
}
 
Example 3
Source File: SigServiceApp.java    From group-signature-client with GNU General Public License v3.0 6 votes vote down vote up
public boolean loadConfig() throws Exception {
    System.out.println("please wait ...");
    Service service;
    try {
        context = new ClassPathXmlApplicationContext("classpath:node/application.xml");
        service = context.getBean(Service.class);
        service.run();
    } catch (Exception e) {
        logger.error("load config failed, error msg: " + e.getMessage());
        throw new Exception("load config failed, error msg: " + e.getMessage());
    }

    credentials = GenCredential.create();

    // channel eth service
    ChannelEthereumService channelService = new ChannelEthereumService();
    channelService.setChannelService(service);
    channelService.setTimeout(5000);
    web3j = Web3j.build(channelService, service.getGroupId());
    return true;
}
 
Example 4
Source File: TestBase.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
@BeforeClass
  public static void setUpBeforeClass() throws Exception {
    // 获取spring配置文件,生成上下文
    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    //   ((ClassPathXmlApplicationContext) context).start();

    Service service = context.getBean(Service.class);
    service.run();

    System.out.println("start...");
    System.out.println("===================================================================");

    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);

    web3j = Web3j.build(channelEthereumService, service.getGroupId());
    // EthBlockNumber ethBlockNumber = web3.ethBlockNumber().send();
//
//    Ok ok = Ok.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
//    address = ok.getContractAddress();
//    blockNumber = ok.getTransactionReceipt().get().getBlockNumber();
//    blockHash = ok.getTransactionReceipt().get().getBlockHash();
//    txHash = ok.getTransactionReceipt().get().getTransactionHash();
  }
 
Example 5
Source File: TestBase.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  // 获取spring配置文件,生成上下文
  context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
  //   ((ClassPathXmlApplicationContext) context).start();

  Service service = context.getBean(Service.class);
  service.run();

  System.out.println("start...");
  System.out.println("===================================================================");

  ChannelEthereumService channelEthereumService = new ChannelEthereumService();
  channelEthereumService.setChannelService(service);
  channelEthereumService.setTimeout(10000);
  web3j = Web3j.build(channelEthereumService, service.getGroupId());
  // EthBlockNumber ethBlockNumber = web3.ethBlockNumber().send();

}
 
Example 6
Source File: TestBase.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  // 获取spring配置文件,生成上下文
  context = new ClassPathXmlApplicationContext("applicationContext.xml");
  //   ((ClassPathXmlApplicationContext) context).start();

  Service service = context.getBean(Service.class);
  service.run();

  System.out.println("start...");
  System.out.println("===================================================================");

  ChannelEthereumService channelEthereumService = new ChannelEthereumService();
  channelEthereumService.setChannelService(service);

  web3j = Web3j.build(channelEthereumService, service.getGroupId());
  // EthBlockNumber ethBlockNumber = web3.ethBlockNumber().send();

}
 
Example 7
Source File: Web3jV2BeanConfig.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
@Bean
public Web3j getWeb3j() throws Exception {
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    Service service = getService();
    service.run();
    channelEthereumService.setChannelService(service);
    // default sync transactions timeout: 30s
    channelEthereumService.setTimeout(30000);
    return Web3j.build(channelEthereumService, service.getGroupId());
}
 
Example 8
Source File: Web3SDKConnector.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
public static Web3j initWeb3j(Service service) throws BrokerException {
    // init web3j with given group id
    try {
        log.info("begin to initialize web3sdk's Web3j, group id: {}", service.getGroupId());
        StopWatch sw = StopWatch.createStarted();

        // special thread for TransactionSucCallback.onResponse, callback from IO thread directly if not setting
        //service.setThreadPool(poolTaskExecutor);
        service.run();

        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        channelEthereumService.setTimeout(service.getConnectSeconds() * 1000);
        Web3j web3j = Web3j.build(channelEthereumService, service.getGroupId());

        // check connect with getNodeVersion command
        NodeVersion.Version version = web3j.getNodeVersion().send().getNodeVersion();
        String nodeVersion = version.getVersion();
        if (StringUtils.isBlank(nodeVersion)
                || !nodeVersion.contains(FISCO_BCOS_2_X_VERSION_PREFIX)) {
            log.error("init web3sdk failed, mismatch FISCO-BCOS version in node: {}", nodeVersion);
            throw new BrokerException(ErrorCode.WEB3SDK_INIT_ERROR);
        }
        chainID = version.getChainID();

        sw.stop();
        log.info("initialize web3sdk success, group id: {} cost: {} ms", service.getGroupId(), sw.getTime());
        return web3j;
    } catch (Exception e) {
        log.error("init web3sdk failed", e);
        throw new BrokerException(ErrorCode.WEB3SDK_INIT_ERROR);
    }
}
 
Example 9
Source File: BcosApp.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
public boolean loadConfig() throws Exception{
	context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	Service service = context.getBean(Service.class);
       service.run();
       ChannelEthereumService channelEthereumService = new ChannelEthereumService();
       channelEthereumService.setChannelService(service);
       web3j = Web3j.build(channelEthereumService,service.getGroupId());
       boolean flag=false;
       if(web3j!=null){
       	flag=true;
       }
       return flag;
}
 
Example 10
Source File: Channel2ServerNeedVerify.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Param: topic");
        return;
    }
    String topic = args[0];
    logger.debug("init Server");
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    Service service = context.getBean(Service.class);
    service.setNeedVerifyTopics(topic);

    PushCallback cb = new PushCallback();
    service.setPushCallback(cb);
    System.out.println("3s...");
    Thread.sleep(1000);
    System.out.println("2s...");
    Thread.sleep(1000);
    System.out.println("1s...");
    Thread.sleep(1000);

    System.out.println("start test");
    System.out.println("===================================================================");

    service.run();
}
 
Example 11
Source File: OkClient.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // init the Service
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(Integer.parseInt(args[0]));
        service.run(); // run the daemon service
        // init the client keys
        keyPair = Keys.createEcKeyPair();
        credentials = Credentials.create(keyPair);

        logger.info("-----> start test !");
        logger.info("init AOMP ChannelEthereumService");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        try {
            web3j = Web3j.build(channelEthereumService, Integer.parseInt(args[0]));
        } catch (Exception e) {
            System.out.println("\nPlease provide groupID in the first paramters");
            System.exit(0);
        }

        if (args.length > 1) {
            if ("deploy".equals(args[1])) {
                deployOk();
            } else {
                String[] params = new String[args.length - 1];
                for (int i = 0; i < params.length; i++) params[i] = args[i + 1];
                testOk(params);
            }
        } else {
            System.out.println("\nPlease choose follow commands:\n deploy, trans or get");
        }
        System.exit(0);
    }
 
Example 12
Source File: GroupManager.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public Web3j init() throws Exception {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();

    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);

    Web3j web3j = Web3j.build(channelEthereumService, service.getGroupId());
    return web3j;
}
 
Example 13
Source File: StartGroup.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        Usage();
    }

    int groupID = Integer.valueOf(args[0]);

    System.out.println(" Start Group operation, groupID: " + groupID);

    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.setGroupId(1);
    service.run();

    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);

    Web3j web3j = Web3j.build(channelEthereumService, 1);

    org.fisco.bcos.web3j.protocol.core.methods.response.StartGroup startGroup =
            web3j.startGroup(groupID).send();
    logger.info("  StartGroup result: {}", startGroup);

    System.out.println(" StartGroup result: " + startGroup.getStatus());

    System.exit(0);
}
 
Example 14
Source File: TableTestClient.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // init the Service
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(Integer.parseInt(args[0]));
        service.run(); // run the daemon service
        // init the client keys
        keyPair = Keys.createEcKeyPair();
        credentials = GenCredential.create(keyPair.getPrivateKey().toString(16));

        logger.info("-----> start test !");
        logger.info("init AOMP ChannelEthereumService");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        channelEthereumService.setTimeout(5 * 1000);
        try {
            web3j = Web3j.build(channelEthereumService, Integer.parseInt(args[0]));
        } catch (Exception e) {
            System.out.println("\nPlease provide groupID in the first paramters");
            System.exit(0);
        }

        if (args.length > 1) {
            if ("deploy".equals(args[1])) {
                deployTableTest();
            } else {
                String[] params = new String[args.length - 1];
                for (int i = 0; i < params.length; i++) params[i] = args[i + 1];
                testTableTest(params);
            }
        } else {
            System.out.println(
                    "\nPlease choose follow commands:\n deploy, create, insert, select, update or remove");
        }
        System.exit(0);
    }
 
Example 15
Source File: Channel2ServerUpdateTopics.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Param: topic");
        return;
    }
    String topic = args[0];
    logger.debug("init Server");
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    PushCallback cb = new PushCallback();
    service.setPushCallback(cb);
    System.out.println("3s...");
    Thread.sleep(1000);
    System.out.println("2s...");
    Thread.sleep(1000);
    System.out.println("1s...");
    Thread.sleep(1000);

    System.out.println("start test");
    System.out.println("===================================================================");
    service.run();

    Thread.sleep(10000);

    System.out.println("set topics");
    System.out.println("===================================================================");
    Set<String> topics = new HashSet<String>();
    topics.add(topic);
    service.setTopics(topics);
    service.updateTopicsToNode();
}
 
Example 16
Source File: GMOkTransaction.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    EncryptType encryptType = new EncryptType(1);
    String groupId = "1";
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("===================================================================");

    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    channelEthereumService.setTimeout(10000);
    Web3j web3 = Web3j.build(channelEthereumService, Integer.parseInt(groupId));
    BigInteger gasPrice = new BigInteger("300000000");
    BigInteger gasLimit = new BigInteger("3000000000");

    Credentials credentials1 =
            GenCredential.create(
                    "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");

    ContractGasProvider contractGasProvider = new StaticGasProvider(gasPrice, gasLimit);
    final Ok okDemo = Ok.deploy(web3, credentials1, contractGasProvider).send();

    for (int i = 0; i < 1; i++) {
        System.out.println("####contract address is: " + okDemo.getContractAddress());
        TransactionReceipt receipt = okDemo.trans(new BigInteger("4")).send();

        System.out.println(" balance = " + okDemo.get().send().intValue());
    }
    System.exit(0);
}
 
Example 17
Source File: Channel2ClientBinNeedVerify.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("param: target topic filename of request");
        return;
    }
    String topic = args[0];
    String filename = args[1];
    Integer count = 10;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    logger.debug("init client");

    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    Service service = context.getBean(Service.class);
    service.run();

    System.out.println("3s ...");
    Thread.sleep(1000);
    System.out.println("2s ...");
    Thread.sleep(1000);
    System.out.println("1s ...");
    Thread.sleep(1000);

    System.out.println("start test");
    System.out.println("===================================================================");

    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        ChannelRequest request = new ChannelRequest();
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);

        /*设置为-128表示为传输二进制*/
        int flag = -128;
        byte[] byteflag = intToByteArray(flag);
        int filelength = filename.length();
        byte[] bytelength = intToByteArray(filelength);
        byte[] bytefilename = filename.getBytes();
        byte[] contentfile = toByteArrFromFile(filename);
        byte[] content =
                byteCat(byteCat(byteCat(byteflag, bytelength), bytefilename), contentfile);
        request.setContent(content);

        logger.info("msg:" + Arrays.toString(content));

        System.out.println(
                df.format(LocalDateTime.now())
                        + " request seq:"
                        + String.valueOf(request.getMessageID())
                        + " content length:"
                        + content.length);

        ChannelResponse response = service.sendChannelMessageForVerifyTopic(request);

        System.out.println(
                df.format(LocalDateTime.now())
                        + "response seq:"
                        + String.valueOf(response.getMessageID())
                        + ", ErrorCode:"
                        + response.getErrorCode()
                        + ", Content:"
                        + response.getContent());
        if (response.getErrorCode() != 0) {
            System.out.println("Error message" + response.getErrorMessage());
        }
    }
}
 
Example 18
Source File: Channel2ClientMulti.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < parameterNum) {
        System.out.println("param: target topic total number of request");
        return;
    }
    String topic = args[0];
    Integer count = Integer.parseInt(args[1]);

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    logger.debug("init client");

    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    Service service = context.getBean(Service.class);
    service.run();

    System.out.println("3s ...");
    Thread.sleep(1000);
    System.out.println("2s ...");
    Thread.sleep(1000);
    System.out.println("1s ...");
    Thread.sleep(1000);

    System.out.println("start test");
    System.out.println("===================================================================");

    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        ChannelRequest request = new ChannelRequest();
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);

        request.setContent("request seq:" + request.getMessageID());

        System.out.println(
                df.format(LocalDateTime.now())
                        + " multicast request seq:"
                        + String.valueOf(request.getMessageID())
                        + ", Content:"
                        + request.getContent());

        service.asyncMulticastChannelMessage2(request);
    }
}
 
Example 19
Source File: Channel2Client.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < parameterNum) {
        System.out.println("param: target topic total number of request");
        return;
    }
    String topic = args[0];
    Integer count = Integer.parseInt(args[1]);

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    logger.debug("init client");
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);

    service.run();

    System.out.println("3s ...");
    Thread.sleep(1000);
    System.out.println("2s ...");
    Thread.sleep(1000);
    System.out.println("1s ...");
    Thread.sleep(1000);

    System.out.println("start test");
    System.out.println("===================================================================");

    ChannelRequest request = new ChannelRequest();
    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);

        String content = "request seq:" + request.getMessageID();

        request.setContent(content.getBytes());

        System.out.println(
                df.format(LocalDateTime.now())
                        + " request seq:"
                        + request.getMessageID()
                        + ", Content:"
                        + request.getContent()
                        + " content:"
                        + Arrays.toString(request.getContentByteArray()));

        ChannelResponse response = service.sendChannelMessage2(request);

        System.out.println(
                df.format(LocalDateTime.now())
                        + "response seq:"
                        + response.getMessageID()
                        + ", ErrorCode:"
                        + response.getErrorCode()
                        + ", Content:"
                        + response.getContent());
    }
}
 
Example 20
Source File: Channel2ClientBin.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("param: target topic filename of request");
        return;
    }
    String topic = args[0];
    String filename = args[1];
    Integer count = 1;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    logger.debug("init client");

    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    Service service = context.getBean(Service.class);
    service.run();

    System.out.println("3s ...");
    Thread.sleep(1000);
    System.out.println("2s ...");
    Thread.sleep(1000);
    System.out.println("1s ...");
    Thread.sleep(1000);

    System.out.println("start test");
    System.out.println("===================================================================");

    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        ChannelRequest request = new ChannelRequest();
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);

        /*设置为-128表示为传输二进制*/
        int flag = -128;
        byte[] byteflag = intToByteArray(flag);
        int filelength = filename.length();
        byte[] bytelength = intToByteArray(filelength);
        byte[] bytefilename = filename.getBytes();
        byte[] contentfile = toByteArrFromFile(filename);
        byte[] content =
                byteCat(byteCat(byteCat(byteflag, bytelength), bytefilename), contentfile);
        request.setContent(content);

        logger.info("msg:" + Arrays.toString(content));

        System.out.println(
                df.format(LocalDateTime.now())
                        + " request seq:"
                        + String.valueOf(request.getMessageID())
                        + " content length:"
                        + content.length);

        ChannelResponse response = service.sendChannelMessage2(request);

        System.out.println(
                df.format(LocalDateTime.now())
                        + "response seq:"
                        + String.valueOf(response.getMessageID())
                        + ", ErrorCode:"
                        + response.getErrorCode()
                        + ", Content:"
                        + response.getContent());
        if (response.getErrorCode() != 0) {
            System.out.println("Error message" + response.getErrorMessage());
        }
    }
}