├── .gitignore ├── .idea ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── modules.xml ├── misc.xml ├── libraries │ ├── Maven__com_h2database_h2_1_4_195.xml │ ├── Maven__javax_cache_cache_api_1_0_0.xml │ ├── Maven__org_jetbrains_annotations_13_0.xml │ ├── Maven__commons_codec_commons_codec_1_6.xml │ ├── Maven__org_gridgain_ignite_shmem_1_0_0.xml │ ├── Maven__org_apache_ignite_ignite_core_2_3_0.xml │ ├── Maven__org_apache_lucene_lucene_core_5_5_2.xml │ ├── Maven__org_apache_ignite_ignite_spring_2_3_0.xml │ ├── Maven__commons_logging_commons_logging_1_1_1.xml │ ├── Maven__org_apache_lucene_lucene_queries_5_5_2.xml │ ├── Maven__org_apache_lucene_lucene_sandbox_5_5_2.xml │ ├── Maven__org_apache_ignite_ignite_indexing_2_3_0.xml │ ├── Maven__org_apache_lucene_lucene_queryparser_5_5_2.xml │ ├── Maven__org_springframework_spring_tx_4_3_7_RELEASE.xml │ ├── Maven__org_springframework_spring_aop_4_3_7_RELEASE.xml │ ├── Maven__org_springframework_spring_core_4_3_7_RELEASE.xml │ ├── Maven__org_springframework_spring_jdbc_4_3_7_RELEASE.xml │ ├── Maven__org_springframework_spring_beans_4_3_7_RELEASE.xml │ ├── Maven__org_apache_lucene_lucene_analyzers_common_5_5_2.xml │ ├── Maven__org_springframework_spring_context_4_3_7_RELEASE.xml │ └── Maven__org_springframework_spring_expression_4_3_7_RELEASE.xml ├── compiler.xml └── MicroServicesExample.iml ├── src └── main │ └── java │ ├── app │ ├── package-info.java │ ├── DataNodeStartup.java │ ├── VehicleServiceNodeStartup.java │ ├── MaintenanceServiceNodeStartup.java │ ├── ExternalTestApp.java │ └── TestAppStartup.java │ ├── services │ ├── package-info.java │ ├── maintenance │ │ ├── package-info.java │ │ ├── common │ │ │ ├── package-info.java │ │ │ ├── Maintenance.java │ │ │ └── MaintenanceService.java │ │ └── MaintenanceServiceImpl.java │ └── vehicles │ │ ├── common │ │ ├── package-info.java │ │ ├── Vehicle.java │ │ └── VehicleService.java │ │ ├── package-info.java │ │ └── VehicleServiceImpl.java │ └── common │ ├── package-info.java │ ├── filters │ ├── DataNodeFilter.java │ ├── VehicleServiceFilter.java │ └── MaintenanceServiceFilter.java │ └── cachestore │ └── SimpleCacheStore.java ├── README.md ├── pom.xml ├── config ├── client-node-config.xml ├── vehicle-service-node-config.xml ├── maintenance-service-node-config.xml └── data-node-config.xml └── MicroServicesExample.iml /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | log/* 3 | tmp/* 4 | target/* -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_h2database_h2_1_4_195.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_cache_cache_api_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jetbrains_annotations_13_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_codec_commons_codec_1_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_gridgain_ignite_shmem_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_ignite_ignite_core_2_3_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_lucene_lucene_core_5_5_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_ignite_ignite_spring_2_3_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_logging_commons_logging_1_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_lucene_lucene_queries_5_5_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_lucene_lucene_sandbox_5_5_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_ignite_ignite_indexing_2_3_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_lucene_lucene_queryparser_5_5_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_tx_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_aop_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_core_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_jdbc_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_beans_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_lucene_lucene_analyzers_common_5_5_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_context_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_expression_4_3_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/app/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Startup files. 20 | */ 21 | package app; 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/services/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Vehicle service source files. 20 | */ 21 | package services; -------------------------------------------------------------------------------- /src/main/java/services/maintenance/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Maintenance service source files. 20 | */ 21 | package services.maintenance; -------------------------------------------------------------------------------- /src/main/java/services/vehicles/common/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Created by dmagda on 1/3/17. 20 | */ 21 | package services.vehicles.common; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apache Ignite Microservices Example 2 | 3 | The example demonstrates how to realize Microservices based architecture on top of Apache Ignite covered in a series of 4 | blog posts: 5 | * Part I: https://www.gridgain.com/resources/blog/running-microservices-top-memory-data-grid-part-i 6 | * Part II: https://www.gridgain.com/resources/blog/microservices-top-memory-data-grid-part-ii 7 | * Part III: https://www.gridgain.com/resources/blog/microservices-top-memory-data-grid-part-iii 8 | 9 | To execute the example do the following: 10 | 11 |
  • 12 | Start one or more instances of data nodes using DataNodeStartup. 13 |
  • 14 |
  • 15 | Start one or more instances of Maintenance service nodes using MaintenanceServiceNodeStartup. 16 |
  • 17 |
  • 18 | Start one or more instances of Vehicle service nodes using VehicleServiceNodeStartup. 19 |
  • 20 |
  • 21 | Execute an internal test application (connects to the microservices using Ignite API) using TestAppStart. 22 |
  • 23 |
  • 24 | Execute an external test application (connects to the microservices using direct sockets API) using ExternalTestApp. 25 |
  • 26 |
    27 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.apache.ignite 8 | microservices-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.ignite 14 | ignite-core 15 | 2.3.0 16 | 17 | 18 | 19 | org.apache.ignite 20 | ignite-spring 21 | 2.3.0 22 | 23 | 24 | 25 | org.apache.ignite 26 | ignite-indexing 27 | 2.3.0 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/services/vehicles/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * The package contains classes that have to be added to the classpath of the nodes where Vehicle service might 20 | * be deployed or which can access the service by means of the service proxy. 21 | */ 22 | package services.vehicles; -------------------------------------------------------------------------------- /src/main/java/services/maintenance/common/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * The package contains classes that have to be added to the classpath of the nodes where Maintenance service might 20 | * be deployed or which can access the service by means of the service proxy. 21 | */ 22 | package services.maintenance.common; -------------------------------------------------------------------------------- /src/main/java/common/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * The package contains classes that have to be added to the classpath of all the nodes - Data Nodes, Service Nodes and 20 | * even to Client Nodes that execute application's logic. 21 | * 22 | * At the moment, the package includes cache and service related filters that are executed for every node that is a part 23 | * of the cluster or will join it. 24 | */ 25 | package common; 26 | 27 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/app/DataNodeStartup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package app; 19 | 20 | import org.apache.ignite.Ignite; 21 | import org.apache.ignite.IgniteException; 22 | import org.apache.ignite.Ignition; 23 | 24 | /** 25 | * A new Data Node will be started in a separate JVM process when this class gets executed. 26 | */ 27 | public class DataNodeStartup { 28 | /** 29 | * Start up a Data Node. 30 | * 31 | * @param args Command line arguments, none required. 32 | * @throws IgniteException If failed. 33 | */ 34 | public static void main(String[] args) throws IgniteException { 35 | Ignite ignite = Ignition.start("config/data-node-config.xml"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/app/VehicleServiceNodeStartup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package app; 19 | 20 | import org.apache.ignite.IgniteException; 21 | import org.apache.ignite.Ignition; 22 | 23 | /** 24 | * A new Vehicle Service Node will be started in a separate JVM process when this class gets executed. 25 | */ 26 | public class VehicleServiceNodeStartup { 27 | /** 28 | * Start up a Vehicle Service Node. 29 | * 30 | * @param args Command line arguments, none required. 31 | * @throws IgniteException If failed. 32 | */ 33 | public static void main(String[] args) throws IgniteException { 34 | Ignition.start("config/vehicle-service-node-config.xml"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/app/MaintenanceServiceNodeStartup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package app; 19 | 20 | import org.apache.ignite.IgniteException; 21 | import org.apache.ignite.Ignition; 22 | 23 | /** 24 | * A new Maintenance Service Node will be started in a separate JVM process when this class gets executed. 25 | */ 26 | public class MaintenanceServiceNodeStartup { 27 | /** 28 | * Start up a Maintenance Node. 29 | * 30 | * @param args Command line arguments, none required. 31 | * @throws IgniteException If failed. 32 | */ 33 | public static void main(String[] args) throws IgniteException { 34 | Ignition.start("config/maintenance-service-node-config.xml"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/services/vehicles/common/Vehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package services.vehicles.common; 18 | 19 | import java.util.Date; 20 | 21 | /** 22 | * POJO used by Vehicle Service. 23 | */ 24 | public class Vehicle { 25 | private String name; 26 | 27 | private Date year; 28 | 29 | private Double price; 30 | 31 | public Vehicle(String name, Date year, Double price) { 32 | this.name = name; 33 | this.year = year; 34 | this.price = price; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public Date getYear() { 46 | return year; 47 | } 48 | 49 | public void setYear(Date year) { 50 | this.year = year; 51 | } 52 | 53 | public Double getPrice() { 54 | return price; 55 | } 56 | 57 | public void setPrice(Double price) { 58 | this.price = price; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/services/maintenance/common/Maintenance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package services.maintenance.common; 18 | 19 | import java.io.Serializable; 20 | import java.util.Date; 21 | import org.apache.ignite.cache.query.annotations.QuerySqlField; 22 | 23 | /** 24 | * POJO for maintenance records. 25 | */ 26 | public class Maintenance implements Serializable { 27 | private int vehicleId; 28 | 29 | private Date date; 30 | 31 | public Maintenance(int vehicleId, Date date) { 32 | this.vehicleId = vehicleId; 33 | this.date = date; 34 | } 35 | 36 | public int getVehicleId() { 37 | return vehicleId; 38 | } 39 | 40 | public void setVehicleId(int vehicleId) { 41 | this.vehicleId = vehicleId; 42 | } 43 | 44 | public Date getDate() { 45 | return date; 46 | } 47 | 48 | public void setDate(Date date) { 49 | this.date = date; 50 | } 51 | 52 | @Override public String toString() { 53 | return "Maintenance{" + 54 | "vehicleId=" + vehicleId + 55 | ", date=" + date + 56 | '}'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/common/filters/DataNodeFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package common.filters; 19 | 20 | import org.apache.ignite.cluster.ClusterNode; 21 | import org.apache.ignite.lang.IgnitePredicate; 22 | 23 | /** 24 | * The filter that has to be set in every cache configuration. 25 | *

    26 | * It will be called upon a cache startup to define a subset of the cluster nodes where cache data 27 | * will be stored - Data Nodes. 28 | *

    29 | * The same filter will be called every time the cluster topology changes which happens when a new cluster node joins 30 | * the cluster or an old one leaves it. When it's executed for the new joined node then depending on the 31 | * filter's execution result the node will be considered as a Data Node and a part of cached data will be rebalanced 32 | * on it or the node will be rejected as a Data Node candidate. 33 | */ 34 | public class DataNodeFilter implements IgnitePredicate{ 35 | /** 36 | * Checks if {@code node} needs to be considered as a Data Node. 37 | * 38 | * @param node Cluster node instance. 39 | * 40 | * @return {@code true} if the node has to be considered as Data Node, {@code false} otherwise. 41 | */ 42 | public boolean apply(ClusterNode node) { 43 | Boolean dataNode = node.attribute("data.node"); 44 | 45 | return dataNode != null && dataNode; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/services/maintenance/common/MaintenanceService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package services.maintenance.common; 18 | 19 | import java.util.Date; 20 | import java.util.List; 21 | import org.apache.ignite.IgniteServices; 22 | import org.apache.ignite.services.Service; 23 | 24 | /** 25 | * Maintenance Service interface which defines service specific methods that are visible to every cluster node that is 26 | * going to interact with the service using {@link IgniteServices} API. In general, the interface is not only used 27 | * by service implementations but also needed for the nodes that will talk to the service by means of service proxy - 28 | * {@link IgniteServices#serviceProxy(String, Class, boolean)}. 29 | */ 30 | public interface MaintenanceService extends Service 31 | { 32 | /** Service name */ 33 | public static final String SERVICE_NAME = "MaintenanceService"; 34 | 35 | /** 36 | * Schedules vehicle maintenance to the nearest available date. 37 | * 38 | * @param vehicleId Vehicle unique ID. 39 | * 40 | * @return The date of the appointment. 41 | */ 42 | public Date scheduleVehicleMaintenance(int vehicleId); 43 | 44 | /** 45 | * Get all maintenance records for a vehicle. 46 | * 47 | * @param vehicleId Vehicle unique ID. 48 | * 49 | * @return Maintenance records. 50 | */ 51 | public List getMaintenanceRecords(int vehicleId); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/services/vehicles/common/VehicleService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package services.vehicles.common; 18 | 19 | import org.apache.ignite.IgniteServices; 20 | import org.apache.ignite.services.Service; 21 | 22 | /** 23 | * Vehicle Service interface which defines service specific methods that are visible to every cluster node that is 24 | * going to interact with the service using {@link IgniteServices} API. In general, the interface is not only used 25 | * by service implementations but also needed for the nodes that will talk to the service by means of service proxy - 26 | * {@link IgniteServices#serviceProxy(String, Class, boolean)}. 27 | */ 28 | public interface VehicleService extends Service { 29 | /** Service name */ 30 | public static final String SERVICE_NAME = "VehicleService"; 31 | 32 | /** 33 | * Calls the service to add a new vehicle. 34 | * 35 | * @param vehicleId Vehicle unique ID. 36 | * @param vehicle Vehicle instance to add. 37 | */ 38 | public void addVehicle(int vehicleId, Vehicle vehicle); 39 | 40 | /** 41 | * Calls the service to get details for a specific vehicle. 42 | * 43 | * @param vehicleId Vehicle unique ID. 44 | */ 45 | public Vehicle getVehicle(int vehicleId); 46 | 47 | /** 48 | * Calls the service to remove a specific vehicle. 49 | * 50 | * @param vehicleId Vehicle unique ID. 51 | */ 52 | public void removeVehicle(int vehicleId); 53 | } 54 | -------------------------------------------------------------------------------- /config/client-node-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 127.0.0.1:47500..47509 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/common/filters/VehicleServiceFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package common.filters; 19 | 20 | import org.apache.ignite.cluster.ClusterNode; 21 | import org.apache.ignite.lang.IgnitePredicate; 22 | import org.apache.ignite.services.ServiceConfiguration; 23 | 24 | /** 25 | * The filter that has to be set in {@link ServiceConfiguration} of Vehicle Service. 26 | *

    27 | * It will be called at the deployment time of Vehicle Service and will define a subset of the nodes where the 28 | * service will be deployed or might be deployed in general - Vehicle Service Nodes. 29 | *

    30 | * The same filter will be called every time the cluster topology changes which happens when a new cluster node joins 31 | * the cluster or an old one leaves it. When it's executed for the new joined node then depending on the filter's 32 | * execution result the node might be considered as a Vehicle Service Node and an instance of the service may be 33 | * deployed there depending on how many service instances have to be deployed cluster wide. 34 | */ 35 | public class VehicleServiceFilter implements IgnitePredicate { 36 | /** 37 | * Checks if {@code node} needs to be considered as a Vehicle Service Node. 38 | * 39 | * @param node Cluster node instance. 40 | * 41 | * @return {@code true} if the node has to be considered as Vehicle Service Node, {@code false} otherwise. 42 | */ 43 | public boolean apply(ClusterNode node) { 44 | Boolean dataNode = node.attribute("vehicle.service.node"); 45 | 46 | return dataNode != null && dataNode; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/common/filters/MaintenanceServiceFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package common.filters; 19 | 20 | import org.apache.ignite.cluster.ClusterNode; 21 | import org.apache.ignite.lang.IgnitePredicate; 22 | import org.apache.ignite.services.ServiceConfiguration; 23 | 24 | /** 25 | * The filter that has to be set in {@link ServiceConfiguration} of Maintenance Service. 26 | *

    27 | * It will be called at the deployment time of Maintenance Service and will define a subset of the nodes where the 28 | * service will be deployed or might be deployed in general - Maintenance Service Nodes. 29 | *

    30 | * The same filter will be called every time the cluster topology changes which happens when a new cluster node joins 31 | * the cluster or an old one leaves it. When it's executed for the new joined node then depending on the filter's 32 | * execution result the node might be considered as a Maintenance Service Node and an instance of the service may be 33 | * deployed there depending on how many service instances have to be deployed cluster wide. 34 | */ 35 | public class MaintenanceServiceFilter implements IgnitePredicate{ 36 | /** 37 | * Checks if {@code node} needs to be considered as a Maintenance Service Node. 38 | * 39 | * @param node Cluster node instance. 40 | * 41 | * @return {@code true} if the node has to be considered as Maintenance Service Node, {@code false} otherwise. 42 | */ 43 | public boolean apply(ClusterNode node) { 44 | Boolean dataNode = node.attribute("maintenance.service.node"); 45 | 46 | return dataNode != null && dataNode; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/app/ExternalTestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package app; 19 | 20 | import java.io.DataOutputStream; 21 | import java.io.ObjectInputStream; 22 | import java.net.Socket; 23 | import java.util.List; 24 | import java.util.Random; 25 | import services.maintenance.common.Maintenance; 26 | 27 | /** 28 | * Sample app that interacts with Maintenance service via plan socket API. The app doesn't use Apache Ignite API 29 | * at all and it's not connected to the cluster somehow. 30 | * 31 | * Make sure to start {@link TestAppStartup} before that populates the cluster with some data. 32 | */ 33 | public class ExternalTestApp { 34 | /** 35 | * Entry point. 36 | * 37 | * @param args Optional. 38 | */ 39 | public static void main(String[] args) { 40 | try { 41 | // Start the TestAppStartup before launching this example. 42 | for (int vehicleId = 0; vehicleId < TestAppStartup.TOTAL_VEHICLES_NUMBER; vehicleId++) { 43 | System.out.println(" >>> Getting maintenance schedule for vehicle:" + vehicleId); 44 | 45 | Socket socket = new Socket("127.0.0.1", 50000); 46 | 47 | DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 48 | 49 | dos.writeInt(vehicleId); 50 | 51 | ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 52 | 53 | List result = (List)ois.readObject(); 54 | 55 | for (Maintenance maintenance : result) 56 | System.out.println(" >>> " + maintenance); 57 | 58 | dos.close(); 59 | ois.close(); 60 | socket.close(); 61 | } 62 | 63 | System.out.println(" >>> Shutting down the application."); 64 | } 65 | catch (Exception e) { 66 | e.printStackTrace(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /MicroServicesExample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/common/cachestore/SimpleCacheStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package common.cachestore; 19 | 20 | import java.util.Map; 21 | import java.util.concurrent.ConcurrentHashMap; 22 | import javax.cache.Cache; 23 | import javax.cache.integration.CacheLoaderException; 24 | import javax.cache.integration.CacheWriterException; 25 | import org.apache.ignite.binary.BinaryObject; 26 | import org.apache.ignite.cache.store.CacheStoreAdapter; 27 | 28 | /** 29 | * Simple cache store implementation that should persist data on disk. For the sake of the example, we use a dummy 30 | * store which is a concurrent hash map. Replace it with an actual persistent store like MySQl, MongoDB satisfying 31 | * your use case. 32 | * 33 | * Alternatively, instead of this sample class you can use the cache store implementation created for relational 34 | * databases, Hibernate, Cassandra and more: 35 | * https://apacheignite.readme.io/docs/persistent-store 36 | */ 37 | public class SimpleCacheStore extends CacheStoreAdapter { 38 | /** Dummy store. Replace it with an real persistent store according to your use case. */ 39 | private Map storeImpl = new ConcurrentHashMap(); 40 | 41 | /** {@inheritDoc} */ 42 | public BinaryObject load(Long key) throws CacheLoaderException { 43 | System.out.println(" >>> Getting Value From Cache Store: " + key); 44 | 45 | return storeImpl.get(key); 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | public void write(Cache.Entry entry) throws CacheWriterException { 50 | System.out.println(" >>> Writing Value To Cache Store: " + entry); 51 | 52 | storeImpl.put(entry.getKey(), entry.getValue()); 53 | } 54 | 55 | /** {@inheritDoc} */ 56 | public void delete(Object key) throws CacheWriterException { 57 | System.out.println(" >>> Removing Key From Cache Store: " + key); 58 | 59 | storeImpl.remove(key); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /.idea/MicroServicesExample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/services/vehicles/VehicleServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package services.vehicles; 18 | 19 | import org.apache.ignite.Ignite; 20 | import org.apache.ignite.IgniteCache; 21 | import org.apache.ignite.resources.IgniteInstanceResource; 22 | import org.apache.ignite.services.ServiceContext; 23 | import services.vehicles.common.Vehicle; 24 | import services.vehicles.common.VehicleService; 25 | 26 | /** 27 | * An implementation of {@link VehicleService} that will be deployed in the cluster. 28 | *

    29 | * The implementation stores vehicle's data in a dedicated distributed cache deployed on Data Nodes. 30 | */ 31 | public class VehicleServiceImpl implements VehicleService { 32 | @IgniteInstanceResource 33 | private Ignite ignite; 34 | 35 | /** Reference to the cache. */ 36 | private IgniteCache vehiclesCache; 37 | 38 | /** {@inheritDoc} */ 39 | public void init(ServiceContext ctx) throws Exception { 40 | System.out.println("Initializing Vehicle Service on node:" + ignite.cluster().localNode()); 41 | 42 | /** 43 | * It's assumed that the cache has already been deployed. To do that, make sure to start Data Nodes with 44 | * a respective cache configuration. 45 | */ 46 | vehiclesCache = ignite.cache("vehicles"); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | public void execute(ServiceContext ctx) throws Exception { 51 | System.out.println("Executing Vehicle Service on node:" + ignite.cluster().localNode()); 52 | 53 | // Some custom logic. 54 | } 55 | 56 | /** {@inheritDoc} */ 57 | public void cancel(ServiceContext ctx) { 58 | System.out.println("Stopping Vehicle Service on node:" + ignite.cluster().localNode()); 59 | 60 | // Some custom logic. 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | public void addVehicle(int vehicleId, Vehicle vehicle) { 65 | vehiclesCache.put(vehicleId, vehicle); 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | public Vehicle getVehicle(int vehicleId) { 70 | return vehiclesCache.get(vehicleId); 71 | } 72 | 73 | /** {@inheritDoc} */ 74 | public void removeVehicle(int vehicleId) { 75 | vehiclesCache.remove(vehicleId); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/app/TestAppStartup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package app; 18 | 19 | import java.util.Calendar; 20 | import java.util.Date; 21 | import java.util.Random; 22 | import org.apache.ignite.Ignite; 23 | import org.apache.ignite.IgniteCache; 24 | import org.apache.ignite.IgniteException; 25 | import org.apache.ignite.Ignition; 26 | import services.maintenance.common.MaintenanceService; 27 | import services.vehicles.common.Vehicle; 28 | import services.vehicles.common.VehicleService; 29 | 30 | /** 31 | * Created by dmagda on 1/6/17. 32 | */ 33 | public class TestAppStartup { 34 | /** Dummy vehicles' names */ 35 | private static final String[] VEHICLES_NAMES = new String [] { 36 | "TOYOTA", "BMW", "MERCEDES", "HYUNDAI", "FORD"}; 37 | 38 | /** Total number of vehicles. */ 39 | public static int TOTAL_VEHICLES_NUMBER = 10; 40 | 41 | /** 42 | * Start up a testing application that connects to the cluster using an Ignite client node. 43 | * 44 | * The app will fill in the caches with sample data and call the services. 45 | * 46 | * @param args Command line arguments, none required. 47 | * @throws IgniteException If failed. 48 | */ 49 | public static void main(String[] args) throws IgniteException { 50 | Ignite ignite = Ignition.start("config/client-node-config.xml"); 51 | 52 | System.out.println("Client node has connected to the cluster"); 53 | 54 | // Getting access to the vehicles cache. At least one Data Node has to be started before so that the cache 55 | // deployed there. To start a Data Node use DataNodeStartup class file or ignite.sh/bat script passing 56 | // data-node-config.xml to it. 57 | IgniteCache vehiclesCache = ignite.cache("vehicles"); 58 | 59 | Random rand = new Random(); 60 | 61 | Calendar calendar = Calendar.getInstance(); 62 | 63 | // Filling it vehicles cache with dummy data. 64 | for (int i = 0; i < TOTAL_VEHICLES_NUMBER; i++) { 65 | 66 | calendar.set(Calendar.MONTH, rand.nextInt(12)); 67 | calendar.set(Calendar.YEAR, 2000 + rand.nextInt(17)); 68 | 69 | vehiclesCache.put(i, new Vehicle( 70 | VEHICLES_NAMES[rand.nextInt(VEHICLES_NAMES.length)], 71 | calendar.getTime(), 72 | (double)(11000 + rand.nextInt(10000)) 73 | )); 74 | } 75 | 76 | System.out.println("Filled in Vehicles cache. Entries number: " + vehiclesCache.size()); 77 | 78 | // Getting access to a remotely deployed Vehicles Service using a service proxy. Proxy has to be used on the 79 | // nodes that don't have the service deployed locally. 80 | VehicleService vehicleService = ignite.services().serviceProxy(VehicleService.SERVICE_NAME, 81 | VehicleService.class, false); 82 | 83 | System.out.println("Getting info for a random vehicle using VehiclesService: " + vehicleService.getVehicle( 84 | rand.nextInt(TOTAL_VEHICLES_NUMBER))); 85 | 86 | MaintenanceService maintenanceService = ignite.services().serviceProxy(MaintenanceService.SERVICE_NAME, 87 | MaintenanceService.class, false); 88 | 89 | int vehicleId = rand.nextInt(TOTAL_VEHICLES_NUMBER); 90 | 91 | Date date = maintenanceService.scheduleVehicleMaintenance(vehicleId); 92 | 93 | System.out.println("Scheduled maintenance service [vehicleID=" + vehicleId + ", " + "date=" + date + ']'); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /config/vehicle-service-node-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 127.0.0.1:47500..47509 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /config/maintenance-service-node-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 127.0.0.1:47500..47509 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /src/main/java/services/maintenance/MaintenanceServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package services.maintenance; 18 | 19 | import java.io.DataInputStream; 20 | import java.io.DataOutputStream; 21 | import java.io.IOException; 22 | import java.io.ObjectOutputStream; 23 | import java.net.ServerSocket; 24 | import java.net.Socket; 25 | import java.util.ArrayList; 26 | import java.util.Date; 27 | import java.util.List; 28 | import javax.cache.Cache; 29 | import org.apache.ignite.Ignite; 30 | import org.apache.ignite.IgniteAtomicSequence; 31 | import org.apache.ignite.IgniteCache; 32 | import org.apache.ignite.cache.query.SqlQuery; 33 | import org.apache.ignite.resources.IgniteInstanceResource; 34 | import org.apache.ignite.services.ServiceContext; 35 | import services.maintenance.common.Maintenance; 36 | import services.maintenance.common.MaintenanceService; 37 | import services.vehicles.common.VehicleService; 38 | 39 | /** 40 | * An implementation of {@link MaintenanceService} that will be deployed in the cluster. 41 | *

    42 | * The implementation stores maintenance records in a dedicated distributed cache deployed on Data Nodes. 43 | */ 44 | public class MaintenanceServiceImpl implements MaintenanceService { 45 | @IgniteInstanceResource 46 | private Ignite ignite; 47 | 48 | /** Reference to the cache. */ 49 | private IgniteCache maintCache; 50 | 51 | /** Maintenance IDs generator */ 52 | private IgniteAtomicSequence sequence; 53 | 54 | /** Processor that accepts requests from external apps that don't use Apache Ignite API. */ 55 | private ExternalCallsProcessor externalCallsProcessor; 56 | 57 | /** {@inheritDoc} */ 58 | public void init(ServiceContext ctx) throws Exception { 59 | System.out.println("Initializing Maintenance Service on node:" + ignite.cluster().localNode()); 60 | 61 | /** 62 | * It's assumed that the cache has already been deployed. To do that, make sure to start Data Nodes with 63 | * a respective cache configuration. 64 | */ 65 | maintCache = ignite.cache("maintenance"); 66 | 67 | /** Processor that accepts requests from external apps that don't use Apache Ignite API. */ 68 | externalCallsProcessor = new ExternalCallsProcessor(); 69 | 70 | externalCallsProcessor.start(); 71 | } 72 | 73 | /** {@inheritDoc} */ 74 | public void execute(ServiceContext ctx) throws Exception { 75 | System.out.println("Executing Maintenance Service on node:" + ignite.cluster().localNode()); 76 | 77 | /** 78 | * Getting the sequence that will be used for IDs generation. 79 | */ 80 | sequence = ignite.atomicSequence("MaintenanceIds", 1, true); 81 | } 82 | 83 | /** {@inheritDoc} */ 84 | public void cancel(ServiceContext ctx) { 85 | System.out.println("Stopping Maintenance Service on node:" + ignite.cluster().localNode()); 86 | 87 | // Stopping external requests processor. 88 | externalCallsProcessor.interrupt(); 89 | } 90 | 91 | /** {@inheritDoc} */ 92 | public Date scheduleVehicleMaintenance(int vehicleId) { 93 | Date date = new Date(); 94 | 95 | // Getting access to VehicleService proxy. The proxy allows to call remotely deployed services. 96 | VehicleService vehicleService = ignite.services().serviceProxy(VehicleService.SERVICE_NAME, 97 | VehicleService.class, false); 98 | 99 | // Calling remote service to double check vehicle's existence. 100 | if (vehicleService.getVehicle(vehicleId) == null) 101 | throw new RuntimeException("Vehicle with provided ID doesn't exist:" + vehicleId); 102 | 103 | // Remembering scheduled appointment. 104 | maintCache.put((int)sequence.getAndIncrement(), new Maintenance(vehicleId, date)); 105 | 106 | return date; 107 | } 108 | 109 | /** {@inheritDoc} */ 110 | public List getMaintenanceRecords(int vehicleId) { 111 | SqlQuery query = new SqlQuery(Maintenance.class, 112 | "WHERE vehicleId = ?").setArgs(vehicleId); 113 | 114 | List> res = maintCache.query(query).getAll(); 115 | 116 | List res2 = new ArrayList(res.size()); 117 | 118 | for (Cache.Entry entry : res) 119 | res2.add(entry.getValue()); 120 | 121 | return res2; 122 | } 123 | 124 | /** 125 | * Thread that accepts request from external applications that don't use Apache Ignite service grid API. 126 | */ 127 | private class ExternalCallsProcessor extends Thread { 128 | /** Server socket to accept external connections. */ 129 | private ServerSocket externalConnect; 130 | 131 | /** {@inheritDoc} */ 132 | @Override public void run() { 133 | try { 134 | externalConnect = new ServerSocket(50000); 135 | 136 | while (!isInterrupted()) { 137 | Socket socket = externalConnect.accept(); 138 | 139 | DataInputStream dis = new DataInputStream(socket.getInputStream()); 140 | 141 | // Getting vehicleId. 142 | int vehicleId = dis.readInt(); 143 | 144 | List result = getMaintenanceRecords(vehicleId); 145 | 146 | ObjectOutputStream dos = new ObjectOutputStream(socket.getOutputStream()); 147 | 148 | // Writing the result into the socket. 149 | dos.writeObject(result); 150 | 151 | dis.close(); 152 | dos.close(); 153 | socket.close(); 154 | } 155 | } 156 | catch (IOException e) { 157 | e.printStackTrace(); 158 | } 159 | } 160 | 161 | /** {@inheritDoc} */ 162 | @Override public void interrupt() { 163 | super.interrupt(); 164 | 165 | try { 166 | externalConnect.close(); 167 | } 168 | catch (IOException e) { 169 | e.printStackTrace(); 170 | } 171 | } 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /config/data-node-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 104 | 105 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 127.0.0.1:47500..47509 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | --------------------------------------------------------------------------------